summaryrefslogtreecommitdiffstats
path: root/kpovmodeler/pmtriangle.cpp
blob: 362ae868675057390d20cb8c89352847a7bcbb3d (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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
/*
**************************************************************************
                                 description
                             --------------------
    copyright            : (C) 2001 by Andreas Zehender
    email                : [email protected]
**************************************************************************

**************************************************************************
*                                                                        *
*  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 "pmtriangle.h"
#include "pmtriangleedit.h"

#include "pmxmlhelper.h"
#include "pmmemento.h"
#include "pmviewstructure.h"
#include "pm3dcontrolpoint.h"
#include "pmvectorcontrolpoint.h"

#include <klocale.h>

const PMVector point0Default = PMVector( -1.0, 0.0, 0.0 );
const PMVector point1Default = PMVector( 1.0, 0.0, 0.0 );
const PMVector point2Default = PMVector( 0.0, 0.0, 1.0 );
const PMVector normal0Default = PMVector( 0.0, 1.0, 0.0 );
const PMVector normal1Default = PMVector( 0.0, 1.0, 0.0 );
const PMVector normal2Default = PMVector( 0.0, 1.0, 0.0 );
const PMVector uvVector0Default = PMVector( 0.0, 0.0 );
const PMVector uvVector1Default = PMVector( 1.0, 0.0 );
const PMVector uvVector2Default = PMVector( 0.5, 1.0 );

PMDefinePropertyClass( PMTriangle, PMTriangleProperty );

class PMPointProperty : public PMPropertyBase
{
public:
   PMPointProperty( ) : PMPropertyBase( "points", PMVariant::Vector )
   {
      m_index = 0;
   }
   virtual int dimensions( ) const { return 1; }
   virtual void setIndex( int /*dimension*/, int index )
   {
      if( index < 0 || index > 2 )
         kdError( PMArea ) << "Illegal index in PMTriangle::PointProperty::setIndex" << endl;
      else
         m_index = index;
   }
   virtual int size( PMObject* /*object*/, int /*dimension*/ ) const
   {
      return 3;
   }
protected:
   virtual bool setProtected( PMObject* obj, const PMVariant& v )
   {
      PMTriangle* p = ( PMTriangle* ) obj;
      p->setPoint( m_index, v.vectorData( ) );
      return true;
   }
   virtual PMVariant getProtected( const PMObject* obj )
   {
      const PMTriangle* p = ( const PMTriangle* ) obj;
      return PMVariant( p->point( m_index ) );
   }

private:
   int m_index;
};

class PMNormalProperty : public PMPropertyBase
{
public:
   PMNormalProperty( ) : PMPropertyBase( "normals", PMVariant::Vector )
   {
      m_index = 0;
   }
   virtual int dimensions( ) const { return 1; }
   virtual void setIndex( int /*dimension*/, int index )
   {
      if( index < 0 || index > 2 )
         kdError( PMArea ) << "Illegal index in PMTriangle::NormalProperty::setIndex" << endl;
      else
         m_index = index;
   }
   virtual int size( PMObject* /*object*/, int /*dimension*/ ) const
   {
      return 3;
   }
protected:
   virtual bool setProtected( PMObject* obj, const PMVariant& v )
   {
      PMTriangle* p = ( PMTriangle* ) obj;
      p->setNormal( m_index, v.vectorData( ) );
      return true;
   }
   virtual PMVariant getProtected( const PMObject* obj )
   {
      const PMTriangle* p = ( const PMTriangle* ) obj;
      return PMVariant( p->normal( m_index ) );
   }

private:
   int m_index;
};

class PMUVVectorProperty : public PMPropertyBase
{
public:
   PMUVVectorProperty( )
         : PMPropertyBase( "uvVectors", PMVariant::Vector )
   {
      m_index = 0;
   }
   virtual int dimensions( ) const { return 1; }
   virtual void setIndex( int /*dimension*/, int index )
   {
      if( index < 0 || index > 2 )
         kdError( PMArea ) << "Illegal index in PMTriangle::UVVectorProperty::setIndex" << endl;
      else
         m_index = index;
   }
   virtual int size( PMObject* /*object*/, int /*dimension*/ ) const
   {
      return 2;
   }
protected:
   virtual bool setProtected( PMObject* obj, const PMVariant& v )
   {
      PMTriangle* p = ( PMTriangle* ) obj;
      p->setUVVector( m_index, v.vectorData( ) );
      return true;
   }
   virtual PMVariant getProtected( const PMObject* obj )
   {
      const PMTriangle* p = ( const PMTriangle* ) obj;
      return PMVariant( p->uvVector( m_index ) );
   }

private:
   int m_index;
};

PMMetaObject* PMTriangle::s_pMetaObject = 0;
PMObject* createNewTriangle( PMPart* part )
{
   return new PMTriangle( part );
}
PMViewStructure* PMTriangle::s_pDefaultViewStructure = 0;

PMTriangle::PMTriangle( PMPart* part )
      : Base( part )
{
   m_point[0] = point0Default;
   m_point[1] = point1Default;
   m_point[2] = point2Default;
   m_normal[0] = normal0Default;
   m_normal[1] = normal1Default;
   m_normal[2] = normal2Default;
   m_smooth = false;
   m_uvVector[0] = uvVector0Default;
   m_uvVector[1] = uvVector1Default;
   m_uvVector[2] = uvVector2Default;
   m_uvEnabled = false;
}

PMTriangle::PMTriangle( const PMTriangle& t )
      : Base( t )
{
   int i;
   for( i = 0; i < 3; i++ )
   {
      m_point[i] = t.m_point[i];
      m_normal[i] = t.m_normal[i];
      m_uvVector[i] = t.m_uvVector[i];
   }
   m_smooth = t.m_smooth;
   m_uvEnabled = t.m_uvEnabled;
}

PMTriangle::~PMTriangle( )
{
}

TQString PMTriangle::description( ) const
{
   if( m_smooth )
      return i18n( "smooth triangle" );
   return i18n( "triangle" );
}

void PMTriangle::serialize( TQDomElement& e, TQDomDocument& doc ) const
{
   e.setAttribute( "point0", m_point[0].serializeXML( ) );
   e.setAttribute( "point1", m_point[1].serializeXML( ) );
   e.setAttribute( "point2", m_point[2].serializeXML( ) );
   e.setAttribute( "normal0", m_normal[0].serializeXML( ) );
   e.setAttribute( "normal1", m_normal[1].serializeXML( ) );
   e.setAttribute( "normal2", m_normal[2].serializeXML( ) );
   e.setAttribute( "smooth", m_smooth );
   e.setAttribute( "uvVector0", m_uvVector[0].serializeXML( ) );
   e.setAttribute( "uvVector1", m_uvVector[1].serializeXML( ) );
   e.setAttribute( "uvVector2", m_uvVector[2].serializeXML( ) );
   e.setAttribute( "uvEnabled", m_uvEnabled );
   Base::serialize( e, doc );
}

void PMTriangle::readAttributes( const PMXMLHelper& h )
{
   m_point[0] = h.vectorAttribute( "point0", point0Default );
   m_point[1] = h.vectorAttribute( "point1", point1Default );
   m_point[2] = h.vectorAttribute( "point2", point2Default );
   m_normal[0] = h.vectorAttribute( "normal0", normal0Default );
   m_normal[1] = h.vectorAttribute( "normal1", normal1Default );
   m_normal[2] = h.vectorAttribute( "normal2", normal2Default );
   m_smooth = h.boolAttribute( "smooth", false );
   m_uvVector[0] = h.vectorAttribute( "uvVector0", uvVector0Default );
   m_uvVector[1] = h.vectorAttribute( "uvVector1", uvVector1Default );
   m_uvVector[2] = h.vectorAttribute( "uvVector2", uvVector2Default );
   m_uvEnabled = h.boolAttribute( "uvEnabled", m_uvEnabled );
   Base::readAttributes( h );
}

PMMetaObject* PMTriangle::tqmetaObject( ) const
{
   if( !s_pMetaObject )
   {
      s_pMetaObject = new PMMetaObject( "Triangle", Base::tqmetaObject( ),
                                        createNewTriangle );
      s_pMetaObject->addProperty(
         new PMTriangleProperty( "smooth", &PMTriangle::setSmoothTriangle,
                         &PMTriangle::isSmoothTriangle ) );
      s_pMetaObject->addProperty( new PMPointProperty( ) );
      s_pMetaObject->addProperty( new PMNormalProperty( ) );
      s_pMetaObject->addProperty( new PMUVVectorProperty( ) );
   }
   return s_pMetaObject;
}

void PMTriangle::setPoint( int i, const PMVector& p )
{
   if( ( i >= 0 ) && ( i <= 2 ) )
   {
      if( p != m_point[i] )
      {
         if( m_pMemento )
            m_pMemento->addData( s_pMetaObject, PMPoint0ID + i, m_point[i] );
         m_point[i] = p;
         m_point[i].resize( 3 );
         setViewStructureChanged( );
      }
   }
   else
      kdError( PMArea ) << "Wrong index in PMTriangle::setPoint\n";
}

PMVector PMTriangle::point( int i ) const
{
   if( ( i >= 0 ) && ( i <= 2 ) )
      return m_point[i];
   else
      kdError( PMArea ) << "Wrong index in PMTriangle::point\n";
   return PMVector( 0.0, 0.0, 0.0 );
}

void PMTriangle::setNormal( int i, const PMVector& p )
{
   if( ( i >= 0 ) && ( i <= 2 ) )
   {
      if( p != m_normal[i] )
      {
         if( m_pMemento )
            m_pMemento->addData( s_pMetaObject, PMNormal0ID + i, m_normal[i] );
         m_normal[i] = p;
         m_normal[i].resize( 3 );
         setViewStructureChanged( );
      }
   }
   else
      kdError( PMArea ) << "Wrong index in PMTriangle::setNormal\n";
}

PMVector PMTriangle::normal( int i ) const
{
   if( ( i >= 0 ) && ( i <= 2 ) )
      return m_normal[i];
   else
      kdError( PMArea ) << "Wrong index in PMTriangle::normal\n";
   return PMVector( 0.0, 0.0, 0.0 );
}

void PMTriangle::setSmoothTriangle( bool on )
{
   if( on != m_smooth )
   {
      if( m_pMemento )
      {
         m_pMemento->addData( s_pMetaObject, PMSmoothID, m_smooth );
         m_pMemento->setDescriptionChanged( );
      }
      m_smooth = on;
      setViewStructureChanged( );
   }
}

PMVector PMTriangle::uvVector( int i ) const
{
   if( i >= 0 && i < 3 )
      return m_uvVector[i];
   else
      kdError( PMArea ) << "Wrong index in PMTriangle::uvVector\n";
   return PMVector( 0.0, 0.0 );
}

void PMTriangle::setUVVector( int i, const PMVector& v )
{
   if( i >= 0 && i < 3 )
   {
      if( v != m_uvVector[i] )
      {
         if( m_pMemento )
            m_pMemento->addData( s_pMetaObject, PMUVVector0ID + i, m_uvVector[i] );
         m_uvVector[i] = v;
         m_uvVector[i].resize( 2 );
      }
   }
   else
      kdError( PMArea ) << "Wrong index in PMTriangle::setNormal\n";
}

void PMTriangle::enableUV( bool yes )
{
   if( yes != m_uvEnabled )
   {
      if( m_pMemento )
         m_pMemento->addData( s_pMetaObject, PMUVEnabledID, m_uvEnabled );
      m_uvEnabled = yes;
   }
}

PMDialogEditBase* PMTriangle::editWidget( TQWidget* tqparent ) const
{
   return new PMTriangleEdit( tqparent );
}

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

   for( ; it.current( ); ++it )
   {
      data = it.current( );
      if( data->objectType( ) == s_pMetaObject )
      {
         switch( data->valueID( ) )
         {
            case PMPoint0ID:
               setPoint( 0, data->vectorData( ) );
               break;
            case PMPoint1ID:
               setPoint( 1, data->vectorData( ) );
               break;
            case PMPoint2ID:
               setPoint( 2, data->vectorData( ) );
               break;
            case PMNormal0ID:
               setNormal( 0, data->vectorData( ) );
               break;
            case PMNormal1ID:
               setNormal( 1, data->vectorData( ) );
               break;
            case PMNormal2ID:
               setNormal( 2, data->vectorData( ) );
               break;
            case PMSmoothID:
               setSmoothTriangle( data->boolData( ) );
               break;
            case PMUVVector0ID:
               setUVVector( 0, data->vectorData( ) );
               break;
            case PMUVVector1ID:
               setUVVector( 1, data->vectorData( ) );
               break;
            case PMUVVector2ID:
               setUVVector( 2, data->vectorData( ) );
               break;
            case PMUVEnabledID:
               enableUV( data->boolData( ) );
               break;
            default:
               kdError( PMArea ) << "Wrong ID in PMTriangle::restoreMemento\n";
               break;
         }
      }
   }
   Base::restoreMemento( s );
}


bool PMTriangle::isDefault( )
{
   if( ( m_point[0] == point0Default )
       && ( m_point[1] == point1Default )
       && ( m_point[2] == point2Default ) )
      return true;
   return false;
}

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

   PMPointArray& points = m_pViewStructure->points( );

   points[0] = m_point[0];
   points[1] = m_point[1];
   points[2] = m_point[2];
}

PMViewStructure* PMTriangle::defaultViewStructure( ) const
{
   if( !s_pDefaultViewStructure )
   {
      s_pDefaultViewStructure = new PMViewStructure( 3, 3 );
      PMPointArray& points = s_pDefaultViewStructure->points( );
      PMLineArray& lines = s_pDefaultViewStructure->lines( );

      points[0] = point0Default;
      points[1] = point1Default;
      points[2] = point2Default;

      lines[0] = PMLine( 0, 1 );
      lines[1] = PMLine( 1, 2 );
      lines[2] = PMLine( 0, 2 );
   }
   return s_pDefaultViewStructure;
}

void PMTriangle::controlPoints( PMControlPointList& list )
{
   PM3DControlPoint* cp;

   cp = new PM3DControlPoint( m_point[0], PMPoint0ID,
                              i18n( "Point 1" ) );
   list.append( cp );
   if( m_smooth )
      list.append( new PMVectorControlPoint( cp, m_normal[0], PMNormal0ID,
                                             i18n( "Normal 1" ) ) );

   cp = new PM3DControlPoint( m_point[1], PMPoint1ID,
                              i18n( "Point 2" ) );
   list.append( cp );
   if( m_smooth )
      list.append( new PMVectorControlPoint( cp, m_normal[1], PMNormal1ID,
                                             i18n( "Normal 2" ) ) );

   cp = new PM3DControlPoint( m_point[2], PMPoint2ID,
                              i18n( "Point 3" ) );
   list.append( cp );
   if( m_smooth )
      list.append( new PMVectorControlPoint( cp, m_normal[2], PMNormal2ID,
                                             i18n( "Normal 3" ) ) );
}

void PMTriangle::controlPointsChanged( PMControlPointList& list )
{
   PMControlPoint* p;
   PMVector p0, p1, p2;
   PMVector n0, n1, n2;
   double normalDirection = 1.0;
   PMVector triangleNormal;
   bool validNormal = false;
   double d;

   for( p = list.first( ); p; p = list.next( ) )
   {
      switch( p->id( ) )
      {
         case PMPoint0ID:
            p0 = ( ( PM3DControlPoint* ) p )->point( );
            break;
         case PMPoint1ID:
            p1 = ( ( PM3DControlPoint* ) p )->point( );
            break;
         case PMPoint2ID:
            p2 = ( ( PM3DControlPoint* ) p )->point( );
            break;
         case PMNormal0ID:
            n0 = ( ( PMVectorControlPoint* ) p )->vector( );
            break;
         case PMNormal1ID:
            n1 = ( ( PMVectorControlPoint* ) p )->vector( );
            break;
         case PMNormal2ID:
            n2 = ( ( PMVectorControlPoint* ) p )->vector( );
            break;
         default:
            break;
      }
   }

   if( m_smooth )
   {
      triangleNormal = PMVector::cross( m_point[1] - m_point[0],
                                        m_point[2] - m_point[0] );
      normalDirection = PMVector::dot( triangleNormal, m_normal[0] );
      if( approxZero( normalDirection  ) )
         normalDirection = PMVector::dot( triangleNormal, m_normal[1] );
      if( approxZero( normalDirection  ) )
         normalDirection = PMVector::dot( triangleNormal, m_normal[2] );
      if( normalDirection < 0 )
         triangleNormal = -triangleNormal;
      if( !approxZero( triangleNormal.abs( ) ) )
      {
         validNormal = true;
         triangleNormal /= triangleNormal.abs( );
      }
   }

   for( p = list.first( ); p; p = list.next( ) )
   {
      if( p->changed( ) )
      {
         switch( p->id( ) )
         {
            case PMPoint0ID:
               if( !( p0.approxEqual( p1 ) || p0.approxEqual( p2 ) ) )
                  setPoint( 0, p0 );
               else
                  ( ( PM3DControlPoint* ) p )->setPoint( m_point[0] );
               break;
            case PMPoint1ID:
               if( !( p1.approxEqual( p0 ) || p1.approxEqual( p2 ) ) )
                  setPoint( 1, p1 );
               else
                  ( ( PM3DControlPoint* ) p )->setPoint( m_point[1] );
               break;
            case PMPoint2ID:
               if( !( p2.approxEqual( p0 ) || p2.approxEqual( p1 ) ) )
                  setPoint( 2, p2 );
               else
                  ( ( PM3DControlPoint* ) p )->setPoint( m_point[2] );
               break;

            case PMNormal0ID:
               if( validNormal )
               {
                  d = PMVector::dot( triangleNormal, n0 );
                  if( d > 0 )
                     setNormal( 0, n0 );
                  else
                  {
                     setNormal( 0, n0 - ( d - 1e-5 ) * triangleNormal );
                     ( ( PMVectorControlPoint* ) p )->setVector( m_normal[0] );
                  }
               }
               else
                  ( ( PMVectorControlPoint* ) p )->setVector( m_normal[0] );
               break;
            case PMNormal1ID:
               if( validNormal )
               {
                  d = PMVector::dot( triangleNormal, n1 );
                  if( d > 0 )
                     setNormal( 1, n1 );
                  else
                  {
                     setNormal( 1, n1 - ( d - 1e-5 ) * triangleNormal );
                     ( ( PMVectorControlPoint* ) p )->setVector( m_normal[1] );
                  }
               }
               else
                  ( ( PMVectorControlPoint* ) p )->setVector( m_normal[1] );
               break;
            case PMNormal2ID:
               if( validNormal )
               {
                  d = PMVector::dot( triangleNormal, n2 );
                  if( d > 0 )
                     setNormal( 2, n2 );
                  else
                  {
                     setNormal( 2, n2 - ( d - 1e-5 ) * triangleNormal );
                     ( ( PMVectorControlPoint* ) p )->setVector( m_normal[2] );
                  }
               }
               else
                  ( ( PMVectorControlPoint* ) p )->setVector( m_normal[2] );
               break;
            default:
               kdError( PMArea ) << "Wrong ID in PMTriangle::controlPointsChanged\n";
               break;
         }
      }
   }
}

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