summaryrefslogtreecommitdiffstats
path: root/kpovmodeler/pmpolynom.cpp
blob: b9d75a13451ee02e7000cd4a443b70a142f44346 (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
/*
**************************************************************************
                                 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 "pmpolynom.h"
#include "pmpolynomedit.h"

#include "pmxmlhelper.h"
#include "pmmemento.h"

#include <klocale.h>

const double c_defaultCoefficients[10] =
{  // Hyperboloid_Y
   1.0,  0.0, 0.0,
   0.0, -1.0, 0.0,
   0.0, 1.0, 0.0,
   -1.0
};
const int c_defaultOrder = 2;
const bool c_defaultSturm = true;
const int c_polynomSize[8] = { 0, 0, 10, 20, 35, 56, 84, 120 };

PMDefinePropertyClass( PMPolynom, PMPolynomProperty );

class PMCoefficientProperty : public PMPropertyBase
{
public:
   PMCoefficientProperty( )
         : PMPropertyBase( "coefficients", PMVariant::Double )
   {
      m_index = 0;
   }
   virtual int dimensions( ) const { return 1; }
   virtual void setIndex( int /*dimension*/, int index )
   {
      m_index = index;
   }
   virtual int size( PMObject* object, int /*dimension*/ ) const
   {
      return c_polynomSize[ ( ( PMPolynom* ) object )->polynomOrder( ) ];
   }
protected:
   virtual bool setProtected( PMObject* obj, const PMVariant& var )
   {
      PMPolynom* p = ( PMPolynom* ) obj;
      PMVector v = p->coefficients( );
      v[m_index] = var.doubleData( );
      p->setCoefficients( v );

      return true;
   }
   virtual PMVariant getProtected( const PMObject* obj )
   {
      PMPolynom* p = ( PMPolynom* ) obj;
      return PMVariant( p->coefficients( )[m_index] );
   }

private:
   int m_index;
};

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

PMPolynom::PMPolynom( PMPart* part )
      : Base( part )
{
   int i;
   m_order = c_defaultOrder;
   m_coefficients = PMVector( 10 );
   for( i = 0; i < 10; i++ )
      m_coefficients[i] = c_defaultCoefficients[i];
   m_sturm = c_defaultSturm;
}

PMPolynom::PMPolynom( const PMPolynom& p )
      : Base( p )
{
   m_order = p.m_order;
   m_coefficients = p.m_coefficients;
   m_sturm = p.m_sturm;
}

PMPolynom::~PMPolynom( )
{
}

TQString PMPolynom::description( ) const
{
   if( m_order == 2 )
      return i18n( "quadric" );
   else if( m_order == 3 )
      return i18n( "cubic" );
   else if( m_order == 4 )
      return i18n( "quartic" );
   return i18n( "polynom" );
}

void PMPolynom::serialize( TQDomElement& e, TQDomDocument& doc ) const
{
   e.setAttribute( "order", m_order );
   e.setAttribute( "coefficients", m_coefficients.serializeXML( ) );
   e.setAttribute( "sturm", m_sturm );
   Base::serialize( e, doc );
}

void PMPolynom::readAttributes( const PMXMLHelper& h )
{
   m_order = h.intAttribute( "order", c_defaultOrder );
   m_coefficients = h.vectorAttribute( "coefficients", m_coefficients );
   m_sturm = h.boolAttribute( "sturm", c_defaultSturm );
   Base::readAttributes( h );
}

PMMetaObject* PMPolynom::tqmetaObject( ) const
{
   if( !s_pMetaObject )
   {
      s_pMetaObject = new PMMetaObject( "Polynom", Base::tqmetaObject( ),
                                        createNewPolynom );
      s_pMetaObject->addProperty(
         new PMPolynomProperty( "polynomOrder", &PMPolynom::setPolynomOrder,
                         &PMPolynom::polynomOrder ) );
      s_pMetaObject->addProperty(
         new PMPolynomProperty( "sturm", &PMPolynom::setSturm,
                         &PMPolynom::sturm ) );

      s_pMetaObject->addProperty( new PMCoefficientProperty( ) );
   }
   return s_pMetaObject;
}

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

PMDialogEditBase* PMPolynom::editWidget( TQWidget* parent ) const
{
   return new PMPolynomEdit( parent );
}

void PMPolynom::setPolynomOrder( int o )
{
   if( ( o < 2 ) || ( o > 7 ) )
   {
      kdError( PMArea ) << "Invalid order in PMPolynom::setPolynomOrder\n";
      o = 2;
   }
   if( o != m_order )
   {
      if( m_pMemento )
      {
         m_pMemento->addData( s_pMetaObject, PMOrderID, m_order );
         if( ( o <= 4 ) || ( m_order <= 4 ) )
            m_pMemento->setDescriptionChanged( );
      }
      m_order = o;
   }
}

void PMPolynom::setCoefficients( const PMVector& c )
{
   if( c.size( ) != ( unsigned ) c_polynomSize[m_order] )
      kdError( PMArea ) << "Wrong vector size in PMPolynom::setCoefficients\n";

   if( c != m_coefficients )
   {
      if( m_pMemento )
         m_pMemento->addData( s_pMetaObject, PMCoefficientsID, m_coefficients );
      m_coefficients = c;
      m_coefficients.resize( c_polynomSize[m_order] );
      //setViewStructureChanged( );
   }
}

void PMPolynom::setSturm( bool s )
{
   if( m_sturm != s )
   {
      if( m_pMemento )
         m_pMemento->addData( s_pMetaObject, PMSturmID, m_sturm );
      m_sturm = s;
   }
}

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

   for( ; it.current( ); ++it )
   {
      data = it.current( );
      if( data->objectType( ) == s_pMetaObject )
      {
         switch( data->valueID( ) )
         {
            case PMOrderID:
               setPolynomOrder( data->intData( ) );
               break;
            case PMCoefficientsID:
               setCoefficients( data->vectorData( ) );
               break;
            case PMSturmID:
               setSturm( data->boolData( ) );
               break;
            default:
               kdError( PMArea ) << "Wrong ID in PMPolynom::restoreMemento\n";
               break;
         }
      }
   }
   Base::restoreMemento( s );
}