summaryrefslogtreecommitdiffstats
path: root/kpovmodeler/pmisosurface.cpp
blob: dd873aa65382c1bca9fa4062a84d35c17f272bb3 (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
/*
**************************************************************************
                                 description
                             --------------------
    copyright            : (C) 2003 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 "pmisosurface.h"

#include "pmxmlhelper.h"
#include "pmisosurfaceedit.h"
#include "pmmemento.h"
#include "pmviewstructure.h"

#include <tdelocale.h>

const PMIsoSurface::ContainedByType c_defaultContainedBy = PMIsoSurface::Box;
const PMVector c_defaultCorner1 = PMVector( -1, -1, -1 );
const PMVector c_defaultCorner2 = PMVector( 1, 1, 1 );
const PMVector c_defaultCenter = PMVector( 0, 0, 0 );
const double c_defaultRadius = 1;

const double c_defaultThreshold = 0.0;
const double c_defaultAccuracy = 0.001;
const double c_defaultMaxGradient = 1.1;
const bool c_defaultEvaluate = false;
const double c_defaultEvaluate0 = 5;
const double c_defaultEvaluate1 = 1.2;
const double c_defaultEvaluate2 = 0.95;
const double c_defaultOpen = false;
const int c_defaultMaxTrace = 1;
const bool c_defaultAllIntersections = false;

PMDefinePropertyClass( PMIsoSurface, PMIsoSurfaceProperty );

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

PMIsoSurface::PMIsoSurface( PMPart* part )
      : Base( part )
{
   m_containedBy = c_defaultContainedBy;
   m_corner1 = c_defaultCorner1;
   m_corner2 = c_defaultCorner2;
   m_center = c_defaultCenter;
   m_radius = c_defaultRadius;
   m_threshold = c_defaultThreshold;
   m_accuracy = c_defaultAccuracy;
   m_maxGradient = c_defaultMaxGradient;
   m_bEvaluate = c_defaultEvaluate;
   m_evaluate[0] = c_defaultEvaluate0;
   m_evaluate[1] = c_defaultEvaluate1;
   m_evaluate[2] = c_defaultEvaluate2;
   m_bOpen = c_defaultOpen;
   m_maxTrace = c_defaultMaxTrace;
   m_bAllIntersections = c_defaultAllIntersections;
}

PMIsoSurface::PMIsoSurface( const PMIsoSurface& b )
      : Base( b )
{
   m_function = b.m_function;
   m_containedBy = b.m_containedBy;
   m_corner1 = b.m_corner1;
   m_corner2 = b.m_corner2;
   m_center = b.m_center;
   m_radius = b.m_radius;
   m_threshold = b.m_threshold;
   m_accuracy = b.m_accuracy;
   m_maxGradient = m_maxGradient;
   m_bEvaluate = b.m_bEvaluate;
   m_evaluate[0] = b.m_evaluate[0];
   m_evaluate[1] = b.m_evaluate[1];
   m_evaluate[2] = b.m_evaluate[2];
   m_bOpen = b.m_bOpen;
   m_maxTrace = b.m_maxTrace;
   m_bAllIntersections = b.m_bAllIntersections;
}

PMIsoSurface::~PMIsoSurface( )
{
}

TQString PMIsoSurface::description( ) const
{
   return i18n( "isosurface" );
}

void PMIsoSurface::serialize( TQDomElement& e, TQDomDocument& doc ) const
{
   TQDomText t = doc.createTextNode( m_function );
   e.appendChild( t );

   if( m_containedBy == Box )
      e.setAttribute( "contained_by", "box" );
   else
      e.setAttribute( "contained_by", "sphere" );
   e.setAttribute( "corner_a", m_corner1.serializeXML( ) );
   e.setAttribute( "corner_b", m_corner2.serializeXML( ) );
   e.setAttribute( "center", m_center.serializeXML( ) );
   e.setAttribute( "radius", m_radius );
   e.setAttribute( "threshold", m_threshold );
   e.setAttribute( "accuracy", m_accuracy );
   e.setAttribute( "max_gradient", m_maxGradient );
   e.setAttribute( "evaluate", m_bEvaluate );
   e.setAttribute( "e0", m_evaluate[0] );
   e.setAttribute( "e1", m_evaluate[1] );
   e.setAttribute( "e2", m_evaluate[2] );
   e.setAttribute( "open", m_bOpen );
   e.setAttribute( "max_trace", m_maxTrace );
   e.setAttribute( "all_intersections", m_bAllIntersections );
   Base::serialize( e, doc );
}

void PMIsoSurface::readAttributes( const PMXMLHelper& h )
{
   TQDomNode e = h.element( ).firstChild( );
   if( e.isText( ) )
      m_function = e.toText( ).data( );

   TQString str = h.stringAttribute( "contained_by", "" );
   if( str == "sphere" )
      m_containedBy = Sphere;
   else
      m_containedBy = Box;

   m_corner1 = h.vectorAttribute( "corner_a", c_defaultCorner1 );
   m_corner2 = h.vectorAttribute( "corner_b", c_defaultCorner1 );
   m_center = h.vectorAttribute( "center", c_defaultCenter );
   m_radius = h.doubleAttribute( "radius", c_defaultRadius );
   m_threshold = h.doubleAttribute( "threshold", c_defaultThreshold );
   m_accuracy = h.doubleAttribute( "accuracy", c_defaultAccuracy );
   m_maxGradient = h.doubleAttribute( "max_gradient", c_defaultMaxGradient );
   m_bEvaluate = h.boolAttribute( "evaluate", c_defaultEvaluate );
   m_evaluate[0] = h.doubleAttribute( "e0", c_defaultEvaluate0 );
   m_evaluate[1] = h.doubleAttribute( "e1", c_defaultEvaluate1 );
   m_evaluate[2] = h.doubleAttribute( "e2", c_defaultEvaluate2 );
   m_bOpen = h.boolAttribute( "open", c_defaultOpen );
   m_maxTrace = h.intAttribute( "max_trace", c_defaultMaxTrace );
   m_bAllIntersections = h.boolAttribute( "all_intersections", c_defaultAllIntersections );

   Base::readAttributes( h );
}

PMMetaObject* PMIsoSurface::metaObject( ) const
{
   if( !s_pMetaObject )
   {
      s_pMetaObject = new PMMetaObject( "IsoSurface", Base::metaObject( ),
                                        createNewIsoSurface );
      // TODO
      /*
      s_pMetaObject->addProperty(
         new PMIsoSurfaceProperty( "corner1", &PMIsoSurface::setCorner1, &PMIsoSurface::corner1 ) );
      s_pMetaObject->addProperty(
         new PMIsoSurfaceProperty( "corner2", &PMIsoSurface::setCorner2, &PMIsoSurface::corner2 ) );
      */
   }
   return s_pMetaObject;
}

void PMIsoSurface::setFunction( const TQString& f )
{
   if( f != m_function )
   {
      if( m_pMemento )
         m_pMemento->addData( s_pMetaObject, FunctionID, m_function );
      m_function = f;
   }
}

void PMIsoSurface::setContainedBy( ContainedByType type )
{
   if( type != m_containedBy )
   {
      if( m_pMemento )
         m_pMemento->addData( s_pMetaObject, ContainedByID, m_containedBy );
      m_containedBy = type;
   }
}

void PMIsoSurface::setCorner1( const PMVector& p )
{
   if( p != m_corner1 )
   {
      if( m_pMemento )
         m_pMemento->addData( s_pMetaObject, Corner1ID, m_corner1 );
      m_corner1 = p;
      m_corner1.resize( 3 );
      //setViewStructureChanged( );
   }
}

void PMIsoSurface::setCorner2( const PMVector& p )
{
   if( p != m_corner2 )
   {
      if( m_pMemento )
         m_pMemento->addData( s_pMetaObject, Corner2ID, m_corner2 );
      m_corner2 = p;
      m_corner2.resize( 3 );
      //setViewStructureChanged( );
   }
}

void PMIsoSurface::setCenter( const PMVector& v )
{
   if( v != m_center )
   {
      if( m_pMemento )
         m_pMemento->addData( s_pMetaObject, CenterID, m_center );
      m_center = v;
   }
}

void PMIsoSurface::setRadius( double r )
{
   if( r != m_radius )
   {
      if( m_pMemento )
         m_pMemento->addData( s_pMetaObject, RadiusID, m_radius );
      m_radius = r;
   }
}
void PMIsoSurface::setThreshold( double d )
{
   if( d != m_threshold )
   {
      if( m_pMemento )
         m_pMemento->addData( s_pMetaObject, ThresholdID, m_threshold );
      m_threshold = d;
   }
}

void PMIsoSurface::setAccuracy( double d )
{
   if( d != m_accuracy )
   {
      if( m_pMemento )
         m_pMemento->addData( s_pMetaObject, AccuracyID, m_accuracy );
      m_accuracy = d;
   }
}

void PMIsoSurface::setMaxGradient( double d )
{
   if( d != m_maxGradient )
   {
      if( m_pMemento )
         m_pMemento->addData( s_pMetaObject, MaxGradientID, m_maxGradient );
      m_maxGradient = d;
   }
}
void PMIsoSurface::setEvaluate( bool yes )
{
   if( yes != m_bEvaluate )
   {
      if( m_pMemento )
         m_pMemento->addData( s_pMetaObject, EvaluateID, m_evaluate );
      m_bEvaluate = yes;
   }
}

void PMIsoSurface::setEvaluateValue( int index, double v )
{
   if( index < 0 || index > 2 )
   {
      kdError( PMArea ) << "Illegal index in PMIsoSurface::setEvaluateValue" << endl;
      return;
   }

   if( v != m_evaluate[index] )
   {
      if( m_pMemento )
      {
         int id = Evaluate0ID;
         switch( index )
         {
            case 0: id = Evaluate0ID; break;
            case 1: id = Evaluate1ID; break;
            case 2: id = Evaluate2ID; break;
            default: break;
         }

         m_pMemento->addData( s_pMetaObject, id, m_evaluate[index] );
      }
      m_evaluate[index] = v;
   }
}

void PMIsoSurface::setOpen( bool yes )
{
   if( yes != m_bOpen )
   {
      if( m_pMemento )
         m_pMemento->addData( s_pMetaObject, OpenID, m_bOpen );
      m_bOpen = yes;
   }
}

void PMIsoSurface::setMaxTrace( int i )
{
   if( i != m_maxTrace )
   {
      if( m_pMemento )
         m_pMemento->addData( s_pMetaObject, MaxTraceID, m_maxTrace );
      m_maxTrace = i;
   }
}

void PMIsoSurface::setAllIntersections( bool yes )
{
   if( yes != m_bAllIntersections )
   {
      if( m_pMemento )
         m_pMemento->addData( s_pMetaObject, AllIntersectionsID, m_bAllIntersections );
      m_bAllIntersections = yes;
   }
}

PMDialogEditBase* PMIsoSurface::editWidget( TQWidget* parent ) const
{
   return new PMIsoSurfaceEdit( parent );
}

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

   for( ; it.current( ); ++it )
   {
      data = it.current( );
      if( data->objectType( ) == s_pMetaObject )
      {
         switch( data->valueID( ) )
         {
            case FunctionID:
               setFunction( data->stringData( ) );
               break;
            case ContainedByID:
               setContainedBy( ( ContainedByType ) data->intData( ) );
               break;
            case Corner1ID:
               setCorner1( data->vectorData( ) );
               break;
            case Corner2ID:
               setCorner2( data->vectorData( ) );
               break;
            case CenterID:
               setCenter( data->vectorData( ) );
               break;
            case RadiusID:
               setRadius( data->doubleData( ) );
               break;
            case ThresholdID:
               setThreshold( data->doubleData( ) );
               break;
            case AccuracyID:
               setAccuracy( data->doubleData( ) );
               break;
            case MaxGradientID:
               setMaxGradient( data->doubleData( ) );
               break;
            case EvaluateID:
               setEvaluate( data->boolData( ) );
               break;
            case Evaluate0ID:
               setEvaluateValue( 0, data->doubleData( ) );
               break;
            case Evaluate1ID:
               setEvaluateValue( 1, data->doubleData( ) );
               break;
            case Evaluate2ID:
               setEvaluateValue( 2, data->doubleData( ) );
               break;
            case OpenID:
               setOpen( data->boolData( ) );
               break;
            case MaxTraceID:
               setMaxTrace( data->intData( ) );
               break;
            case AllIntersectionsID:
               setAllIntersections( data->boolData( ) );
               break;
            default:
               kdError( PMArea ) << "Wrong ID in PMIsoSurface::restoreMemento\n";
               break;
         }
      }
   }
   Base::restoreMemento( s );
}


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