| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | * Copyright (c) 2000-2022 Inria | ||
| 3 | * All rights reserved. | ||
| 4 | * | ||
| 5 | * Redistribution and use in source and binary forms, with or without | ||
| 6 | * modification, are permitted provided that the following conditions are met: | ||
| 7 | * | ||
| 8 | * * Redistributions of source code must retain the above copyright notice, | ||
| 9 | * this list of conditions and the following disclaimer. | ||
| 10 | * * Redistributions in binary form must reproduce the above copyright notice, | ||
| 11 | * this list of conditions and the following disclaimer in the documentation | ||
| 12 | * and/or other materials provided with the distribution. | ||
| 13 | * * Neither the name of the ALICE Project-Team nor the names of its | ||
| 14 | * contributors may be used to endorse or promote products derived from this | ||
| 15 | * software without specific prior written permission. | ||
| 16 | * | ||
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| 18 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
| 21 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| 22 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| 23 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| 24 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| 25 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| 26 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| 27 | * POSSIBILITY OF SUCH DAMAGE. | ||
| 28 | * | ||
| 29 | * Contact: Bruno Levy | ||
| 30 | * | ||
| 31 | * https://www.inria.fr/fr/bruno-levy | ||
| 32 | * | ||
| 33 | * Inria, | ||
| 34 | * Domaine de Voluceau, | ||
| 35 | * 78150 Le Chesnay - Rocquencourt | ||
| 36 | * FRANCE | ||
| 37 | * | ||
| 38 | */ | ||
| 39 | |||
| 40 | #include <geogram_gfx/gui/arc_ball.h> | ||
| 41 | #include <geogram/basic/quaternion.h> | ||
| 42 | |||
| 43 | namespace GEO { | ||
| 44 | |||
| 45 | /******************************************************************/ | ||
| 46 | |||
| 47 | ✗ | ArcBall::ArcBall() : | |
| 48 | ✗ | center_(0.0,0.0), | |
| 49 | ✗ | radius_(1.0), | |
| 50 | ✗ | last_point_(0.0,0.0) | |
| 51 | { | ||
| 52 | ✗ | constrain_x_ = false ; | |
| 53 | ✗ | constrain_y_ = false ; | |
| 54 | ✗ | grabbed_ = false ; | |
| 55 | ✗ | y_inverted_ = false; | |
| 56 | ✗ | } | |
| 57 | |||
| 58 | ✗ | vec3 ArcBall::constrain_vector( | |
| 59 | const vec3& vector, | ||
| 60 | const vec3& axis | ||
| 61 | ) const { | ||
| 62 | ✗ | vec3 result = normalize(vector - dot(vector, axis)*axis) ; | |
| 63 | ✗ | return result ; | |
| 64 | } | ||
| 65 | |||
| 66 | ✗ | vec3 ArcBall::mouse_to_sphere( | |
| 67 | const vec2& p_in | ||
| 68 | ) { | ||
| 69 | |||
| 70 | ✗ | vec2 p(p_in.x / 1.96, p_in.y / 1.96) ; | |
| 71 | |||
| 72 | ✗ | if(y_inverted_) { | |
| 73 | ✗ | p.y = -p.y; | |
| 74 | } | ||
| 75 | |||
| 76 | double mag; | ||
| 77 | ✗ | vec2 v2 = (center_ - p) / radius_ ; | |
| 78 | ✗ | vec3 v3( v2.x, v2.y, 0.0 ); | |
| 79 | |||
| 80 | ✗ | mag = dot(v2, v2); | |
| 81 | |||
| 82 | ✗ | if ( mag > 1.0 ) { | |
| 83 | ✗ | v3 = normalize(v3) ; | |
| 84 | } | ||
| 85 | else { | ||
| 86 | ✗ | v3 = vec3(v3.x, v3.y, -sqrt(1.0 - mag)) ; | |
| 87 | } | ||
| 88 | |||
| 89 | /* Now we add constraints - X takes precedence over Y */ | ||
| 90 | ✗ | if ( constrain_x_ ) { | |
| 91 | ✗ | v3 = constrain_vector( v3, vec3( 1.0, 0.0, 0.0 )); | |
| 92 | ✗ | } else if ( constrain_y_ ) { | |
| 93 | ✗ | v3 = constrain_vector( v3, vec3( 0.0, 1.0, 0.0 )); | |
| 94 | } | ||
| 95 | |||
| 96 | ✗ | return v3 ; | |
| 97 | } | ||
| 98 | |||
| 99 | |||
| 100 | ✗ | void ArcBall::grab(const vec2& value) { | |
| 101 | ✗ | last_point_ = value ; | |
| 102 | ✗ | grabbed_ = true ; | |
| 103 | ✗ | } | |
| 104 | |||
| 105 | ✗ | void ArcBall::release(const vec2&) { | |
| 106 | ✗ | grabbed_ = false ; | |
| 107 | ✗ | } | |
| 108 | |||
| 109 | ✗ | void ArcBall::drag(const vec2& value) { | |
| 110 | ✗ | if(!grabbed_) { | |
| 111 | ✗ | return ; | |
| 112 | } | ||
| 113 | |||
| 114 | ✗ | vec2 new_point = value ; | |
| 115 | |||
| 116 | ✗ | vec3 v0 = mouse_to_sphere( last_point_ ); | |
| 117 | ✗ | vec3 v1 = mouse_to_sphere( new_point ); | |
| 118 | ✗ | vec3 cross_ = cross(v0, v1) ; | |
| 119 | |||
| 120 | ✗ | Quaternion update_quaternion(cross_, -dot(v0, v1) ); | |
| 121 | ✗ | mat4 update_matrix = update_quaternion.to_matrix() ; | |
| 122 | |||
| 123 | ✗ | matrix_ = matrix_ * update_matrix ; | |
| 124 | ✗ | last_point_ = new_point ; | |
| 125 | } | ||
| 126 | |||
| 127 | ✗ | void ArcBall::set_value(const mat4& m) { | |
| 128 | ✗ | matrix_ = m ; | |
| 129 | ✗ | } | |
| 130 | |||
| 131 | ✗ | void ArcBall::reset() { | |
| 132 | ✗ | matrix_.load_identity(); | |
| 133 | ✗ | grabbed_ = false; | |
| 134 | ✗ | last_point_ = vec2(0.0, 0.0); | |
| 135 | ✗ | } | |
| 136 | |||
| 137 | /*****************************************************/ | ||
| 138 | |||
| 139 | } | ||
| 140 |