1 ///
2 /// Copyright (c) 2012, Texas A&M University
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
7 /// are met:
8 ///
9 ///  * Redistributions of source code must retain the above copyright
10 ///    notice, this list of conditions and the following disclaimer.
11 ///  * Redistributions in binary form must reproduce the above
12 ///    copyright notice, this list of conditions and the following
13 ///    disclaimer in the documentation and/or other materials provided
14 ///    with the distribution.
15 ///  * Neither the name of Texas A&M University nor the names of its
16 ///    contributors may be used to endorse or promote products derived
17 ///    from this software without specific prior written permission.
18 ///
19 /// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 /// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 /// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 /// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 /// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 /// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 /// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 /// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 /// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 /// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 /// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 /// POSSIBILITY OF SUCH DAMAGE.
31 ///
32 /// The following software was written as part of a collaboration with the
33 /// University of South Carolina, Interdisciplinary Mathematics Institute.
34 ///
35 
36 /// @file cloudTransformTool.cpp
37 /// @details the implementation of class CloudTransformTool
38 /// @author Yue Li and Matthew Hielsberg
39 
40 #include <pcl/apps/point_cloud_editor/common.h>
41 #include <pcl/apps/point_cloud_editor/cloudTransformTool.h>
42 #include <pcl/apps/point_cloud_editor/cloud.h>
43 
44 const float CloudTransformTool::DEFAULT_SCALE_FACTOR_ = 1.14;
45 const float CloudTransformTool::DEFAULT_TRANSLATE_FACTOR_ = 0.001f;
46 
47 
CloudTransformTool(CloudPtr cloud_ptr)48 CloudTransformTool::CloudTransformTool (CloudPtr cloud_ptr)
49   : cloud_ptr_(std::move(cloud_ptr)), x_(0), y_(0), scale_factor_(DEFAULT_SCALE_FACTOR_),
50     translate_factor_(DEFAULT_TRANSLATE_FACTOR_)
51 {
52   setIdentity(transform_matrix_);
53 }
54 
~CloudTransformTool()55 CloudTransformTool::~CloudTransformTool ()
56 {
57 }
58 
59 void
start(int x,int y,BitMask,BitMask)60 CloudTransformTool::start (int x, int y, BitMask, BitMask)
61 {
62   x_ = x;
63   y_ = y;
64 
65   trackball_.start(x, y);
66 }
67 
68 void
update(int x,int y,BitMask modifiers,BitMask buttons)69 CloudTransformTool::update (int x, int y, BitMask modifiers, BitMask buttons)
70 {
71   if (!cloud_ptr_)
72     return;
73   if (!(buttons & LEFT))
74     return;
75 
76   float transform[MATRIX_SIZE];
77 
78   int dx = (x - x_);
79   int dy = (y - y_);
80   if (dx == 0 && dy == 0)
81     return;
82   trackball_.update(x, y);
83   if (modifiers & CTRL)
84     getTranslateMatrix(dx, dy, transform);
85   else if (modifiers & ALT)
86     getZTranslateMatrix(dy, transform);
87   else if (modifiers & SHFT)
88     getScaleMatrix(dy, transform);
89   else
90     trackball_.getRotationMatrix(transform);
91 
92   cloud_ptr_ -> multMatrix(transform);
93 
94   x_ = x;
95   y_ = y;
96 }
97 
98 void
getTranslateMatrix(int dx,int dy,float * matrix)99 CloudTransformTool::getTranslateMatrix (int dx, int dy, float* matrix)
100 {
101   setIdentity(matrix);
102   float scale = 1.0f / cloud_ptr_-> getScalingFactor();
103   matrix[12] = float(dx) * translate_factor_ * scale;
104   matrix[13] = float(-dy) * translate_factor_ * scale;
105 }
106 
107 void
getZTranslateMatrix(int dy,float * matrix)108 CloudTransformTool::getZTranslateMatrix (int dy, float* matrix)
109 {
110   setIdentity(matrix);
111   matrix[14] = float(dy) * translate_factor_ / cloud_ptr_-> getScalingFactor();
112 }
113 
114 void
getScaleMatrix(int dy,float * matrix) const115 CloudTransformTool::getScaleMatrix (int dy, float* matrix) const
116 {
117   setIdentity(matrix);
118   float scale = dy > 0 ? scale_factor_ : 1.0 / scale_factor_;
119   for (unsigned int i = 0; i < MATRIX_SIZE-1; i+=MATRIX_SIZE_DIM+1)
120     matrix[i] = scale;
121 }
122 
123