summaryrefslogtreecommitdiffstats
path: root/kpovmodeler/pm2dcontrolpoint.cpp
blob: 66e7a85a6809137191ff3799d28fc9c2f04ca765 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
**************************************************************************
                                 description
                             --------------------
    copyright            : (C) 2000-2001 by Andreas Zehender
    email                : zehender@kde.org
**************************************************************************

**************************************************************************
*                                                                        *
*  This program is free software; you can redistribute it and/or modify  *
*  it under the terms of the GNU General Public License as published by  *
*  the Free Software Foundation; either version 2 of the License, or     *
*  (at your option) any later version.                                   *
*                                                                        *
**************************************************************************/

#include "pm2dcontrolpoint.h"
#include "pmmath.h"
#include <math.h>

PM2DControlPoint::PM2DControlPoint( const PMVector& point,
                                    PM2DControlPoint::CPType type, int id,
                                    const QString& description )
      : PMControlPoint( id, description )
{
   m_point = point;
   m_type = type;
   m_thirdCoordinate = 0;
   m_scale = 1.0;
   m_pBasePoint = 0;
   m_pLatheLink = 0;
}

void PM2DControlPoint::graphicalChangeStarted( )
{
   QPtrListIterator<PM2DControlPoint> it( m_linkedPoints );
   for( ; *it; ++it )
      if( !( *it )->selected( ) )
         ( *it )->graphicalChangeStarted( );

   m_original2DPoint = m_point;
   m_originalPoint = to3D( m_point );
}

void PM2DControlPoint::graphicalChange( const PMVector& startPoint,
                                        const PMVector& /*viewNormal*/,
                                        const PMVector& endPoint )
{
   if( m_pBasePoint && m_pBasePoint->selected( ) )
      return;
   
   m_point = to2D( m_originalPoint + endPoint - startPoint );
   
   if( m_pLatheLink && m_pLatheLink->selected( ) )
   {
      PM2DControlPoint* ll = m_pLatheLink;
      PMVector op = ll->to2D( ll->m_originalPoint + endPoint - startPoint );
      
      m_point = to2D( m_originalPoint + endPoint - startPoint );
      if( ( m_point - m_original2DPoint ).abs( ) <
          ( op - ll->m_original2DPoint ).abs( ) )
         m_point = op;
   }

   QPtrListIterator<PM2DControlPoint> it( m_linkedPoints );
   for( ; *it; ++it )
   {
      ( *it )->m_point = m_point + ( *it )->m_original2DPoint
                         - m_original2DPoint;
      ( *it )->setChanged( );
   }
}

void PM2DControlPoint::setBasePoint( PM2DControlPoint* p )
{
   if( p != m_pBasePoint )
   {
      if( m_pBasePoint )
         m_pBasePoint->removeLinkedPoint( this );
      m_pBasePoint = p;
      if( m_pBasePoint )
         m_pBasePoint->addLinkedPoint( this );
   }
}

void PM2DControlPoint::snapToGrid( )
{
   int i;
   double d = moveGrid( );
   bool diff = false;
   PMVector change( 2 );
   
   if( m_pBasePoint && m_pBasePoint->selected( ) )
   {
      m_point -= m_pBasePoint->m_point;
      diff = true;
   }

   if( !approxZero( d ) )
   {
      for( i = 0; i < 2; i++ )
      {
         change[i] = -m_point[i];
         m_point[i] = rint( m_point[i] / d ) * d;
         change[i] += m_point[i];
      }
   }

   if( diff )
      m_point += m_pBasePoint->m_point;

   QPtrListIterator<PM2DControlPoint> it( m_linkedPoints );
   for( ; *it; ++it )
   {
      ( *it )->m_point += change;
      ( *it )->setChanged( );
   }
   
   setChanged( );
}

PMVector PM2DControlPoint::to2D( const PMVector& v ) const
{
   PMVector result( 2 );
   switch( m_type )
   {
      case PM2DXY:
         result[0] = v[0];
         result[1] = v[1];
         break;
      case PM2DXZ:
         result[0] = v[0];
         result[1] = v[2];
         break;
      case PM2DYZ:
         result[0] = v[1];
         result[1] = v[2];
         break;
      case PM2DYX:
         result[0] = v[1];
         result[1] = v[0];
         break;
      case PM2DZX:
         result[0] = v[2];
         result[1] = v[0];
         break;
      case PM2DZY:
         result[0] = v[2];
         result[1] = v[1];
         break;
   }
   if( !approxZero( m_scale ) )
      result /= m_scale;
   return result;
}

PMVector PM2DControlPoint::to3D( const PMVector& v ) const
{
   PMVector vec( v * m_scale );
   PMVector result( 3 );
   switch( m_type )
   {
      case PM2DXY:
         result[0] = vec[0];
         result[1] = vec[1];
         result[2] = m_thirdCoordinate;
         break;
      case PM2DXZ:
         result[0] = vec[0];
         result[1] = m_thirdCoordinate;
         result[2] = vec[1];
         break;
      case PM2DYZ:
         result[0] = m_thirdCoordinate;
         result[1] = vec[0];
         result[2] = vec[1];
         break;
      case PM2DYX:
         result[1] = vec[0];
         result[0] = vec[1];
         result[2] = m_thirdCoordinate;
         break;
      case PM2DZX:
         result[2] = vec[0];
         result[0] = vec[1];
         result[1] = m_thirdCoordinate;
         break;
      case PM2DZY:
         result[2] = vec[0];
         result[1] = vec[1];
         result[0] = m_thirdCoordinate;
         break;
   }
   return result;
}

void PM2DControlPoint::addLinkedPoint( PM2DControlPoint* p )
{
   if( !m_linkedPoints.containsRef( p ) )
      m_linkedPoints.append( p );
}

void PM2DControlPoint::removeLinkedPoint( PM2DControlPoint* p )
{
   m_linkedPoints.removeRef( p );
}