summaryrefslogtreecommitdiffstats
path: root/kpovmodeler/pmwarp.cpp
blob: 166777ce7cc0464cd3df73936387a3e0e7b08d81 (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
/*
**************************************************************************
                                 description
                             --------------------
    copyright            : (C) 2000-2002 by Luis Carvalho
    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 "pmxmlhelper.h"
#include "pmvector.h"
#include "pmwarp.h"
#include "pmwarpedit.h"
#include "pmmemento.h"
#include "pmenumproperty.h"

#include <klocale.h>

const PMVector directionDefault = PMVector( 1.0, 0.0, 0.0 );
const PMVector offsetDefault = PMVector( 0.0, 0.0, 0.0 );
const PMVector flipDefault = PMVector( 0.0, 0.0, 0.0 );
const PMVector locationDefault = PMVector( 0.0, 0.0, 0.0 );
const double radiusDefault = 0;
const double strengthDefault = 0;
const double falloffDefault = 0;
const bool inverseDefault = false;
const PMVector repeatDefault = PMVector( 0.0, 0.0, 0.0 );
const PMVector turbulenceDefault = PMVector( 0.0, 0.0, 0.0 );
const PMVector valueVectorDefault = PMVector( 0.0, 0.0, 0.0 );
const int octavesDefault = 6;
const double omegaDefault = 0.5;
const double lambdaDefault = 2.0;
const PMVector orientationDefault = PMVector( 0.0, 0.0, 1.0 );
const double distExpDefault = 0.0;
const double majorRadiusDefault = 1.0;

PMDefinePropertyClass( PMWarp, PMWarpProperty );
PMDefineEnumPropertyClass( PMWarp, PMWarp::PMWarpType, PMWarpTypeProperty );

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

PMWarp::PMWarp( PMPart* part )
      : Base( part )
{
   m_warpType = PMWarp::Repeat;
   m_direction = directionDefault;
   m_offset = offsetDefault;
   m_flip = flipDefault;
   m_location = locationDefault;
   m_radius = radiusDefault;
   m_strength = strengthDefault;
   m_falloff = falloffDefault;
   m_inverse = inverseDefault;
   m_repeat = repeatDefault;
   m_turbulence = turbulenceDefault;
   m_valueVector = valueVectorDefault;
   m_octaves = octavesDefault;
   m_omega = omegaDefault;
   m_lambda = lambdaDefault;
   m_orientation = orientationDefault;
   m_distExp = distExpDefault;
   m_majorRadius = majorRadiusDefault;
}

PMWarp::PMWarp( const PMWarp& w )
      : Base( w )
{
   m_warpType = w.m_warpType;
   m_direction = w.m_direction;
   m_offset = w.m_offset;
   m_flip = w.m_flip;
   m_location = w.m_location;
   m_radius = w.m_radius;
   m_strength = w.m_strength;
   m_falloff = w.m_falloff;
   m_inverse = w.m_inverse;
   m_repeat = w.m_repeat;
   m_turbulence = w.m_turbulence;
   m_valueVector = w.m_valueVector;
   m_octaves = w.m_octaves;
   m_omega = w.m_omega;
   m_lambda = w.m_lambda;
   m_orientation = w.m_orientation;
   m_distExp = w.m_distExp;
   m_majorRadius = w.m_majorRadius;
}

PMWarp::~PMWarp( )
{
}

TQString PMWarp::description( ) const
{
   return i18n( "warp" );
}

void PMWarp::serialize( TQDomElement& e, TQDomDocument& /*doc*/ ) const
{
   bool mapping = false;

   switch( m_warpType )
   {
      case PMWarp::Repeat:
         e.setAttribute( "warp_type", "repeat");
         e.setAttribute( "direction", m_direction.serializeXML( ) );
         e.setAttribute( "offset", m_offset.serializeXML( ) );
         e.setAttribute( "flip", m_flip.serializeXML( ) );
         break;
      case PMWarp::BlackHole:
         e.setAttribute( "warp_type", "black hole");
         e.setAttribute( "location", m_location.serializeXML( ) );
         e.setAttribute( "radius", m_radius );
         e.setAttribute( "strength", m_strength );
         e.setAttribute( "falloff", m_falloff );
         e.setAttribute( "inverse", m_inverse );
         e.setAttribute( "repeat", m_repeat.serializeXML( ) );
         e.setAttribute( "turbulence", m_turbulence.serializeXML( ) );
         break;
      case PMWarp::Turbulence:
         e.setAttribute( "warp_type", "turbulence");
         e.setAttribute( "turbulence", m_valueVector.serializeXML( ) );
         e.setAttribute( "octaves", m_octaves );
         e.setAttribute( "omega", m_omega );
         e.setAttribute( "lambda", m_lambda );
         break;
      case PMWarp::Cylindrical:
         mapping = true;
         e.setAttribute( "warp_type", "cylindrical" );
         break;
      case PMWarp::Spherical:
         mapping = true;
         e.setAttribute( "warp_type", "spherical" );
         break;
      case PMWarp::Toroidal:
         mapping = true;
         e.setAttribute( "warp_type", "toroidal" );
         e.setAttribute( "major_radius", m_majorRadius );
         break;
      case PMWarp::Planar:
         mapping = true;
         e.setAttribute( "warp_type", "planar" );
         break;
   }

   if ( mapping )
   {
      e.setAttribute( "orientation", m_orientation.serializeXML( ) );
      e.setAttribute( "dist_exp", m_distExp );
   }
}

void PMWarp::readAttributes( const PMXMLHelper& h )
{
   bool mapping = false;
   TQString str = h.stringAttribute( "warp_type", "repeat" );

   if( str == "repeat" )
   {
      m_warpType = PMWarp::Repeat;
      m_direction = h.vectorAttribute( "direction", directionDefault );
      m_offset = h.vectorAttribute( "offset", offsetDefault );
      m_flip = h.vectorAttribute( "flip", flipDefault );
   }
   else if( str == "black hole" )
   {
      m_warpType = PMWarp::BlackHole;
      m_location = h.vectorAttribute( "location", locationDefault );
      m_radius = h.doubleAttribute( "radius", radiusDefault );
      m_strength = h.doubleAttribute( "strength", strengthDefault );
      m_falloff = h.doubleAttribute( "falloff", falloffDefault );
      m_inverse = h.boolAttribute( "inverse", inverseDefault );
      m_repeat = h.vectorAttribute( "repeat", repeatDefault );
      m_turbulence = h.vectorAttribute( "turbulence", turbulenceDefault );
   }
   else if( str == "turbulence" )
   {
      m_warpType = PMWarp::Turbulence;
      m_valueVector = h.vectorAttribute( "turbulence", valueVectorDefault );
      m_octaves = h.intAttribute( "octaves", octavesDefault );
      m_omega = h.doubleAttribute( "omega", omegaDefault );
      m_lambda = h.doubleAttribute( "lambda", lambdaDefault );
   }
   else if( str == "cylindrical" )
   {
      mapping = true;
      m_warpType = PMWarp::Cylindrical;
   }
   else if( str == "spherical" )
   {
      mapping = true;
      m_warpType = PMWarp::Spherical;
   }
   else if( str == "toroidal" )
   {
      mapping = true;
      m_warpType = PMWarp::Toroidal;
      m_majorRadius = h.doubleAttribute( "major_radius", majorRadiusDefault );
   }
   else if( str == "planar" )
   {
      mapping = true;
      m_warpType = PMWarp::Planar;
   }

   if( mapping )
   {
      m_orientation = h.vectorAttribute( "orientation", orientationDefault );
      m_distExp = h.doubleAttribute( "dist_exp", distExpDefault );
   }
}

PMMetaObject* PMWarp::metaObject( ) const
{
   if( !s_pMetaObject )
   {
      s_pMetaObject = new PMMetaObject( "Warp", Base::metaObject( ),
                                        createNewWarp );

      PMWarpTypeProperty* p = new PMWarpTypeProperty(
         "warpType", &PMWarp::setWarpType, &PMWarp::warpType );
      p->addEnumValue( "Repeat", Repeat );
      p->addEnumValue( "BlackHole", BlackHole );
      p->addEnumValue( "Turbulence", Turbulence );
      p->addEnumValue( "Cylindrical", Cylindrical );
      p->addEnumValue( "Spherical", Spherical );
      p->addEnumValue( "Toroidal", Toroidal );
      p->addEnumValue( "Planar", Planar );
      s_pMetaObject->addProperty( p );

      s_pMetaObject->addProperty(
         new PMWarpProperty( "direction", &PMWarp::setDirection, &PMWarp::direction ) );
      s_pMetaObject->addProperty(
         new PMWarpProperty( "offset", &PMWarp::setOffset, &PMWarp::offset ) );
      s_pMetaObject->addProperty(
         new PMWarpProperty( "flip", &PMWarp::setFlip, &PMWarp::flip ) );
      s_pMetaObject->addProperty(
         new PMWarpProperty( "location", &PMWarp::setLocation, &PMWarp::location ) );
      s_pMetaObject->addProperty(
         new PMWarpProperty( "radius", &PMWarp::setRadius, &PMWarp::radius ) );
      s_pMetaObject->addProperty(
         new PMWarpProperty( "strength", &PMWarp::setStrength, &PMWarp::strength ) );
      s_pMetaObject->addProperty(
         new PMWarpProperty( "falloff", &PMWarp::setFalloff, &PMWarp::falloff ) );
      s_pMetaObject->addProperty(
         new PMWarpProperty( "inverse", &PMWarp::setInverse, &PMWarp::inverse ) );
      s_pMetaObject->addProperty(
         new PMWarpProperty( "repeat", &PMWarp::setRepeat, &PMWarp::repeat ) );
      s_pMetaObject->addProperty(
         new PMWarpProperty( "turbulence", &PMWarp::setTurbulence, &PMWarp::turbulence ) );
      s_pMetaObject->addProperty(
         new PMWarpProperty( "valueVector", &PMWarp::setValueVector, &PMWarp::valueVector ) );
      s_pMetaObject->addProperty(
         new PMWarpProperty( "octaves", &PMWarp::setOctaves, &PMWarp::octaves ) );
      s_pMetaObject->addProperty(
         new PMWarpProperty( "omega", &PMWarp::setOmega, &PMWarp::omega ) );
      s_pMetaObject->addProperty(
         new PMWarpProperty( "lambda", &PMWarp::setLambda, &PMWarp::lambda ) );
      s_pMetaObject->addProperty(
         new PMWarpProperty( "orientation", &PMWarp::setOrientation, &PMWarp::orientation ) );
      s_pMetaObject->addProperty(
         new PMWarpProperty( "dist_exp", &PMWarp::setDistExp, &PMWarp::distExp ) );
      s_pMetaObject->addProperty(
         new PMWarpProperty( "major_radius", &PMWarp::setMajorRadius, &PMWarp::majorRadius ) );
   }
   return s_pMetaObject;
}

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

void PMWarp::setWarpType( PMWarpType c )
{
   if( c != m_warpType )
   {
      if( m_pMemento )
         m_pMemento->addData( s_pMetaObject, PMWarpTypeID, m_warpType );
      m_warpType = c;
   }
}

void PMWarp::setDirection( const PMVector& c )
{
   if( c != m_direction )
   {
      if( m_pMemento )
         m_pMemento->addData( s_pMetaObject, PMDirectionID, m_direction );
      m_direction = c;
   }
}

void PMWarp::setOffset( const PMVector& c )
{
   if( c != m_offset )
   {
      if( m_pMemento )
         m_pMemento->addData( s_pMetaObject, PMOffsetID, m_offset );
      m_offset = c;
   }
}

void PMWarp::setFlip( const PMVector& c )
{
   if( c != m_flip )
   {
      if( m_pMemento )
         m_pMemento->addData( s_pMetaObject, PMFlipID, m_flip );
      m_flip = c;
   }
}

void PMWarp::setLocation( const PMVector& c )
{
   if( c != m_location )
   {
      if( m_pMemento )
         m_pMemento->addData( s_pMetaObject, PMLocationID, m_location );
      m_location = c;
   }
}

void PMWarp::setRadius( const double c )
{
   if( c != m_radius )
   {
      if( m_pMemento )
         m_pMemento->addData( s_pMetaObject, PMRadiusID, m_radius );
      m_radius = c;
   }
}

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

void PMWarp::setFalloff( const double c )
{
   if( c != m_falloff )
   {
      if( m_pMemento )
         m_pMemento->addData( s_pMetaObject, PMFalloffID, m_falloff );
      m_falloff = c;
   }
}

void PMWarp::setInverse( const bool c )
{
   if( c != m_inverse )
   {
      if( m_pMemento )
         m_pMemento->addData( s_pMetaObject, PMInverseID, m_inverse );
      m_inverse = c;
   }
}

void PMWarp::setRepeat( const PMVector& c )
{
   if( c != m_repeat )
   {
      if( m_pMemento )
         m_pMemento->addData( s_pMetaObject, PMRepeatID, m_repeat );
      m_repeat = c;
   }
}

void PMWarp::setTurbulence( const PMVector& c )
{
   if( c != m_turbulence )
   {
      if( m_pMemento )
         m_pMemento->addData( s_pMetaObject, PMTurbulenceID, m_turbulence );
      m_turbulence = c;
   }
}

void PMWarp::setValueVector( const PMVector& c )
{
   if( c != m_valueVector )
   {
      if( m_pMemento )
         m_pMemento->addData( s_pMetaObject, PMValueVectorID, m_valueVector );
      m_valueVector = c;
   }
}

void PMWarp::setOctaves( const int c )
{
   if( c != m_octaves )
   {
      if( m_pMemento )
         m_pMemento->addData( s_pMetaObject, PMOctavesID, m_octaves );
      m_octaves = c;
   }
}

void PMWarp::setOmega( const double c )
{
   if( c != m_omega )
   {
      if( m_pMemento )
         m_pMemento->addData( s_pMetaObject, PMOmegaID, m_omega );
      m_omega = c;
   }
}

void PMWarp::setLambda( const double c )
{
   if( c != m_lambda )
   {
      if( m_pMemento )
         m_pMemento->addData( s_pMetaObject, PMLambdaID, m_lambda );
      m_lambda = c;
   }
}

void PMWarp::setOrientation( const PMVector& v )
{
   if ( v != m_orientation )
   {
      if ( m_pMemento )
         m_pMemento->addData( s_pMetaObject, PMOrientationID, m_orientation );
      m_orientation = v;
   }
}

void PMWarp::setDistExp( const double c )
{
   if ( c != m_distExp )
   {
      if ( m_pMemento )
         m_pMemento->addData( s_pMetaObject, PMDistExpID, m_distExp );
      m_distExp = c;
   }
}

void PMWarp::setMajorRadius( const double c )
{
   if ( c != m_majorRadius )
   {
      if ( m_pMemento )
         m_pMemento->addData( s_pMetaObject, PMMajorRadiusID, m_majorRadius );
      m_majorRadius = c;
   }
}

PMDialogEditBase* PMWarp::editWidget( TQWidget* parent ) const
{
   return new PMWarpEdit( parent );
}

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

   for( ; it.current( ); ++it )
   {
      data = it.current( );
      if( data->objectType( ) == s_pMetaObject )
      {
         switch( data->valueID( ) )
         {
            case PMWarpTypeID:
               setWarpType( ( PMWarpType )data->intData( ) );
               break;
            case PMDirectionID:
               setDirection( data->vectorData( ) );
               break;
            case PMOffsetID:
               setOffset( data->vectorData( ) );
               break;
            case PMFlipID:
               setFlip( data->vectorData( ) );
               break;
            case PMLocationID:
               setLocation( data->vectorData( ) );
               break;
            case PMRadiusID:
               setRadius( data->doubleData( ) );
               break;
            case PMStrengthID:
               setStrength( data->doubleData( ) );
               break;
            case PMFalloffID:
               setFalloff( data->doubleData( ) );
               break;
            case PMInverseID:
               setInverse( data->boolData( ) );
               break;
            case PMRepeatID:
               setRepeat( data->vectorData( ) );
               break;
            case PMTurbulenceID:
               setTurbulence( data->vectorData( ) );
               break;
            case PMValueVectorID:
               setValueVector( data->vectorData( ) );
               break;
            case PMOctavesID:
               setOctaves( data->intData( ) );
               break;
            case PMOmegaID:
               setOmega( data->doubleData( ) );
               break;
            case PMLambdaID:
               setLambda( data->doubleData( ) );
               break;
            case PMOrientationID:
               setOrientation( data->vectorData( ) );
               break;
            case PMDistExpID:
               setDistExp( data->doubleData( ) );
               break;
            case PMMajorRadiusID:
               setMajorRadius( data->doubleData( ) );
               break;
            default:
               kdError( PMArea ) << "Wrong ID in PMWarp::restoreMemento\n";
               break;
         }
      }
   }
   Base::restoreMemento( s );
}