summaryrefslogtreecommitdiffstats
path: root/kpovmodeler/pmblobcylinder.cpp
blob: 687ce13296dcb5d1fb8e76b495f0401f828cab5d (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
**************************************************************************
                                 description
                             --------------------
    copyright            : (C) 2002 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 "pmblobcylinder.h"

#include "pmxmlhelper.h"
#include "pmboxedit.h"
#include "pmmemento.h"
#include "pm3dcontrolpoint.h"
#include "pmdefaults.h"
#include "pmdistancecontrolpoint.h"
#include "pmblobcylinderedit.h"

#include <klocale.h>

PMDefinePropertyClass( PMBlobCylinder, PMBlobCylinderProperty );

PMMetaObject* PMBlobCylinder::s_pMetaObject = 0;
PMObject* createNewBlobCylinder( PMPart* part )
{
   return new PMBlobCylinder( part );
}

const double c_defaultRadius = 0.5;
const PMVector c_defaultEnd1 = PMVector ( 0, 0.5, 0 );
const PMVector c_defaultEnd2 = PMVector ( 0, -0.5, 0 );
const double c_defaultStrength = 1.0;

/** default cylinder structure */
PMViewStructure* PMBlobCylinder::s_pDefaultViewStructure = 0;
int PMBlobCylinder::s_vStep = c_defaultBlobCylinderVSteps;
int PMBlobCylinder::s_uStep = c_defaultBlobCylinderUSteps;
int PMBlobCylinder::s_parameterKey = 0;


PMBlobCylinder::PMBlobCylinder( PMPart* part )
      : Base( part )
{
   m_end1 = c_defaultEnd1;
   m_end2 = c_defaultEnd2;
   m_radius = c_defaultRadius;
   m_strength = c_defaultStrength;
}

PMBlobCylinder::PMBlobCylinder( const PMBlobCylinder& c )
      : Base( c )
{
   m_end1 = c.m_end1;
   m_end2 = c.m_end2;
   m_radius = c.m_radius;
   m_strength = c.m_strength;
}

PMBlobCylinder::~PMBlobCylinder( )
{
}

QString PMBlobCylinder::description( ) const
{
   return i18n( "blob cylinder" );
}

void PMBlobCylinder::serialize( QDomElement& e, QDomDocument& doc ) const
{
   e.setAttribute( "end_a", m_end1.serializeXML( ) );
   e.setAttribute( "end_b", m_end2.serializeXML( ) );
   e.setAttribute( "radius", m_radius );
   e.setAttribute( "strength", m_strength );
   Base::serialize( e, doc );
}

void PMBlobCylinder::readAttributes( const PMXMLHelper& h )
{
   m_end1 = h.vectorAttribute( "end_a", c_defaultEnd1 );
   m_end2 = h.vectorAttribute( "end_b", c_defaultEnd2 );
   m_radius = h.doubleAttribute( "radius", c_defaultRadius );
   m_strength = h.doubleAttribute( "strength", c_defaultStrength );
   Base::readAttributes( h );
}

PMMetaObject* PMBlobCylinder::metaObject( ) const
{
   if( !s_pMetaObject )
   {
      s_pMetaObject = new PMMetaObject( "BlobCylinder", Base::metaObject( ),
                                        createNewBlobCylinder );
      s_pMetaObject->addProperty(
         new PMBlobCylinderProperty( "end1", &PMBlobCylinder::setEnd1,
                         &PMBlobCylinder::end1 ) );
      s_pMetaObject->addProperty(
         new PMBlobCylinderProperty( "end2", &PMBlobCylinder::setEnd2,
                         &PMBlobCylinder::end2 ) );
      s_pMetaObject->addProperty(
         new PMBlobCylinderProperty( "radius", &PMBlobCylinder::setRadius,
                         &PMBlobCylinder::radius ) );
      s_pMetaObject->addProperty(
         new PMBlobCylinderProperty( "strength", &PMBlobCylinder::setStrength,
                         &PMBlobCylinder::strength ) );
   }
   return s_pMetaObject;
}

void PMBlobCylinder::setEnd1( const PMVector& p )
{
   if( p != m_end1 )
   {
      if( m_pMemento )
         m_pMemento->addData( s_pMetaObject, PMEnd1ID, m_end1 );
      m_end1 = p;
      m_end1.resize( 3 );
      setViewStructureChanged( );
   }
}

void PMBlobCylinder::setEnd2( const PMVector& p )
{
   if( p != m_end2 )
   {
      if( m_pMemento )
         m_pMemento->addData( s_pMetaObject, PMEnd2ID, m_end2 );
      m_end2 = p;
      m_end2.resize( 3 );
      setViewStructureChanged( );
   }
}

void PMBlobCylinder::setRadius( double radius )
{
   if( m_radius != radius )
   {
      if( m_pMemento )
        m_pMemento->addData( s_pMetaObject, PMRadiusID, m_radius );
      m_radius = radius;
      setViewStructureChanged( );
   }
}

void PMBlobCylinder::setStrength( double s )
{
   if( s != m_strength )
   {
      if( m_pMemento )
         m_pMemento->addData( s_pMetaObject, PMStrengthID, m_strength );
      m_strength = s;
   }
}

PMDialogEditBase* PMBlobCylinder::editWidget( QWidget* parent ) const
{
   return new PMBlobCylinderEdit( parent );
}

void PMBlobCylinder::restoreMemento( PMMemento* s )
{
   PMMementoDataIterator it( s );
   PMMementoData* data;

   for( ; it.current( ); ++it )
   {
      data = it.current( );
      if( data->objectType( ) == s_pMetaObject )
      {
         switch( data->valueID( ) )
         {
            case PMEnd1ID:
               setEnd1( data->vectorData( ) );
               break;
            case PMEnd2ID:
               setEnd2( data->vectorData( ) );
               break;
            case PMRadiusID:
               setRadius( data->doubleData( ) );
               break;
            case PMStrengthID:
               setStrength( data->doubleData( ) );
               break;
            default:
               kdError( PMArea ) << "Wrong ID in PMBlobCylinder::restoreMemento\n";
               break;
         }
      }
   }
   Base::restoreMemento( s );
}

bool PMBlobCylinder::isDefault( )
{
   if( ( m_end1 == c_defaultEnd1 ) && ( m_end2 == c_defaultEnd2 )
       && ( m_radius == c_defaultRadius )
       && globalDetail( ) )
      return true;
   return false;
}

void PMBlobCylinder::createViewStructure( )
{
   if( !m_pViewStructure )
   {
      m_pViewStructure = new PMViewStructure( defaultViewStructure ( ) );
      m_pViewStructure->points( ).detach( );
   }

   int uStep = (int)( ( (float)s_uStep / 2 ) * ( displayDetail( ) + 1 ) );
   int vStep = (int)( ( (float)s_vStep / 2 ) * ( displayDetail( ) + 1 ) );
   unsigned ptsSize = vStep * uStep * 2 + 2;
   unsigned lineSize = vStep * uStep * 4 + vStep;

   if( ptsSize != m_pViewStructure->points( ).size( ) )
      m_pViewStructure->points( ).resize( ptsSize );

   createPoints( m_pViewStructure->points( ), m_end1, m_end2, m_radius, uStep, vStep );

   if( lineSize != m_pViewStructure->lines( ).size( ) )
   {
      m_pViewStructure->lines( ).detach( );
      m_pViewStructure->lines( ).resize( lineSize );
      createLines( m_pViewStructure->lines( ), uStep, vStep );
   }
}

PMViewStructure* PMBlobCylinder::defaultViewStructure( ) const
{
   if( !s_pDefaultViewStructure || s_pDefaultViewStructure->parameterKey( ) != viewStructureParameterKey( ) )
   {
      delete s_pDefaultViewStructure;
      s_pDefaultViewStructure = 0;
      int uStep = (int)( ( (float)s_uStep / 2 ) * ( displayDetail( ) + 1 ) );
      int vStep = (int)( ( (float)s_vStep / 2 ) * ( displayDetail( ) + 1 ) );

      s_pDefaultViewStructure =
         new PMViewStructure( vStep * uStep * 2 + 2,
                              vStep * uStep * 4 + vStep );

      createPoints( s_pDefaultViewStructure->points( ), c_defaultEnd1,
                    c_defaultEnd2, c_defaultRadius, uStep, vStep );

      createLines( s_pDefaultViewStructure->lines( ), uStep, vStep );
   }
   return s_pDefaultViewStructure;
}

void PMBlobCylinder::createLines( PMLineArray& lines, int uStep, int vStep )
{
   int u, v;
   int offset = 0;

   // horizontal lines
   for( u = 0; u < ( uStep * 2 ); u++ )
   {
      for( v = 0; v < ( vStep - 1 ); v++ )
         lines[offset + v] =
            PMLine( u * vStep + v + 1, u * vStep + v + 2 );
      lines[offset + vStep - 1] =
         PMLine( u * vStep + 1, u * vStep + vStep );

      offset += vStep;
   }

   // vertical lines
   // lines at the "north pole"
   for( v = 0; v < vStep; v++ )
      lines[offset + v] = PMLine( 0, v + 1 );
   offset += vStep;

   for( v = 0; v < vStep; v++ )
   {
      for( u = 0; u < ( 2 * uStep - 1 ); u++ )
      {
         lines[offset + u] =
            PMLine( u * vStep + v + 1, ( u + 1 ) * vStep + v + 1 );
      }
      offset += ( 2 * uStep - 1 );
   }
   // lines at the "south pole"
   for( v = 0; v < vStep; v++ )
      lines[offset + v] = PMLine( ( 2 * uStep - 1 ) * vStep + v + 1,
                                    2 * uStep * vStep + 1 );
   // offset += vStep;
}

void PMBlobCylinder::createPoints( PMPointArray& points, const PMVector& end1,
                                   const PMVector& end2, double radius, int uStep, int vStep )
{
   double uRadStep = M_PI / uStep / 2.0;
   double vRadStep = 2 * M_PI / vStep;
   double du = uRadStep;

   if( radius < 0 )
      radius = -radius;

   PMVector pointAt = end2 - end1;
   double pl = pointAt.abs( );
   if( approxZero( pl ) )
      pointAt = PMVector( 0.0, 0.0, 1.0 );
   else
      pointAt /= pl;

   PMMatrix rotation = PMMatrix::rotation( pointAt, vRadStep );
   PMVector ortho = pointAt.orthogonal( );
   ortho /= ortho.abs( );

   points[0] = PMPoint( end1 - pointAt * radius );
   points[vStep * uStep * 2 + 1] = PMPoint( end2 + pointAt * radius );

   int u, v;
   for( u = 0; u < uStep; u++ )
   {
      PMVector end = ortho * radius * sin( du );
      PMVector pv = pointAt * radius * cos( du );
      PMVector e1 = end1 - pv;
      PMVector e2 = end2 + pv;

      for( v = 0; v < vStep; v++ )
      {
         points[u * vStep + v + 1] = PMPoint( e1 + end );
         points[vStep * uStep * 2 - ( u + 1 ) * vStep + v + 1]
            = PMPoint( e2 + end );
         end = rotation * end;
      }
      du += uRadStep;
   }
}

void PMBlobCylinder::controlPoints( PMControlPointList & list )
{
   PMVector center, angle1, angle2;
   center = m_end1 - m_end2;
   double pl = center.abs( );
   if( approxZero( pl ) )
      center = PMVector( 0.0, 1.0, 0.0 );
   else
      center /= pl;

   angle1 = center.orthogonal( );
   angle2 = PMVector::cross( center, angle1 );

   PM3DControlPoint* pb = new PM3DControlPoint( m_end1, PMEnd1ID, i18n( "End 1" ) );
   list.append( pb );
   list.append( new PM3DControlPoint( m_end2, PMEnd2ID, i18n( "End 2" ) ) );
   list.append( new PMDistanceControlPoint( pb, angle1, m_radius, PMRadiusID, i18n( "Radius (1)" ) ) );
   list.append( new PMDistanceControlPoint( pb, angle2, m_radius, PMRadiusID, i18n( "Radius (2)" ) ) );
}


void PMBlobCylinder::controlPointsChanged( PMControlPointList & list )
{
   PMControlPoint* p;
   bool pointChanged = false;
   bool radiusChanged = false;

   for( p = list.first( ); p; p = list.next( ) )
   {
      if( p->changed( ) )
      {
         switch( p->id( ) )
         {
            case PMEnd1ID:
               setEnd1( ( ( PM3DControlPoint *) p)->point( ) );
               pointChanged = true;
               break;
            case PMEnd2ID:
               setEnd2( ( ( PM3DControlPoint *) p)->point( ) );
               pointChanged = true;
               break;
            case PMRadiusID:
               setRadius( ( ( PMDistanceControlPoint *) p)->distance( ) );
               radiusChanged = true;
               break;
            default:
               kdError( PMArea ) << "Wrong ID in PMBlobCylinder::controlPointsChanged\n";
               break;
         }
      }
   }

   if( pointChanged )
   {
      PMVector center, angle1, angle2;
      bool firstPoint = true;

      center = m_end1 - m_end2;
      double pl = center.abs( );
      if( approxZero( pl ) )
         center = PMVector( 0.0, 1.0, 0.0 );
      else
        center /= pl;

      angle1 = center.orthogonal( );
      angle2 = PMVector::cross( center, angle1 );

      for( p = list.first( ); p; p = list.next( ) )
         if( p->id( ) == PMRadiusID )
         {
            if( firstPoint )
            {
               ( ( PMDistanceControlPoint *) p)->setDirection( angle1 );
               firstPoint = false;
            }
            else
               ( ( PMDistanceControlPoint *) p)->setDirection( angle2 );
         }
    }

   if( radiusChanged )
     for( p = list.first( ); p; p = list.next( ) )
        if( p->id( ) == PMRadiusID )
           ( ( PMDistanceControlPoint *) p)->setDistance( m_radius );
}

void PMBlobCylinder::setUSteps( int u )
{
   if( u >= 2 )
   {
      s_uStep = u;
      if( s_pDefaultViewStructure )
      {
         delete s_pDefaultViewStructure;
         s_pDefaultViewStructure = 0;
      }
   }
   else
      kdDebug( PMArea ) << "PMBlobCylinder::setUSteps: U must be greater than 1\n";
   s_parameterKey++;
}

void PMBlobCylinder::setVSteps( int v )
{
   if( v >= 4 )
   {
      s_vStep = v;
      if( s_pDefaultViewStructure )
      {
         delete s_pDefaultViewStructure;
         s_pDefaultViewStructure = 0;
      }
   }
   else
      kdDebug( PMArea ) << "PMBlobCylinder::setVSteps: V must be greater than 3\n";
   s_parameterKey++;
}

void PMBlobCylinder::cleanUp( ) const
{
   if( s_pDefaultViewStructure )
      delete s_pDefaultViewStructure;
   s_pDefaultViewStructure = 0;
   if( s_pMetaObject )
   {
      delete s_pMetaObject;
      s_pMetaObject = 0;
   }
   Base::cleanUp( );
}