File: BasicSCProperties.h

package info (click to toggle)
sofa-framework 1.0~beta4-9
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 88,688 kB
  • ctags: 27,205
  • sloc: cpp: 151,126; ansic: 2,387; xml: 581; sh: 417; makefile: 67
file content (329 lines) | stat: -rw-r--r-- 10,775 bytes parent folder | download | duplicates (5)
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
/***************************************************************************
        BasicSCProperties.h  -  (was StructuralComponentProperties.h)
                             -------------------
    begin             : Wed Aug 8 2001
    copyright         : (C) 2001 TIMC (Emmanuel Promayon, Matthieu Chabanas)
    email             : Emmanuel.Promayon@imag.fr
    Date              : $Date: 2006/10/17 14:33:21 $
    Version           : $Revision: 1.7 $
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef BASICSCPROPERTIES_H
#define BASICSCPROPERTIES_H

#include "Properties.h"
#include <RenderingMode.h>

/**
 * A special class to manage the basic structural component properties.
 *
 * You should derive from this class a StucturalComponentProperties class and use
 * it to implement your own custom stuff.
 * This is a pure virtual class.
 *
 * @author Emmanuel Promayon
 */
class BasicSCProperties  : public Properties {
public:
    /** Default color settings
    */
    enum Color {
        DEFAULT,
        RED, /**< full complete flashy red*/
        GREEN,
        BLUE,
        GRAY,
        OTHER
    };

    /** use a Color constant to set the color */
    BasicSCProperties(PhysicalModel *p, const Color c) : Properties(p) {
        alloc();
        setColor(c);
        setMode(RenderingMode::WIREFRAME_AND_SURFACE);
    };
	BasicSCProperties(PhysicalModel * p, xmlNodePtr node);
    BasicSCProperties(PhysicalModel *p, const Color c, const std::string n) : Properties(p,n) {
        alloc();
        setColor(c);
        setMode(RenderingMode::WIREFRAME_AND_SURFACE);
    };
    /** use a double[3] array to set the color */
    BasicSCProperties(PhysicalModel *p, const double * rgb) : Properties(p)  {
        alloc();
        setRGB(rgb);
        setAlpha(1.0);
        setMode(RenderingMode::WIREFRAME_AND_SURFACE);
    };
    BasicSCProperties(PhysicalModel *p, const double * rgb, const std::string n) : Properties(p,n)  {
        alloc();
        setRGB(rgb);
        setAlpha(1.0);
        setMode(RenderingMode::WIREFRAME_AND_SURFACE);
    };
    /** use 3 floats to set the color */
    BasicSCProperties(PhysicalModel *p, const double r, const double g, const double b) : Properties(p)  {
        alloc();
        setRGB(r,g,b);
        setAlpha(1.0);
        setMode(RenderingMode::WIREFRAME_AND_SURFACE);
    };
    BasicSCProperties(PhysicalModel *p, const double r, const double g, const double b, const std::string n) : Properties(p,n)  {
        alloc();
        setRGB(r,g,b);
        setAlpha(1.0);
        setMode(RenderingMode::WIREFRAME_AND_SURFACE);
    };
    /** defaultcolor is gray */
    BasicSCProperties(PhysicalModel *p) : Properties(p)  {
        alloc();
        setColor(DEFAULT);
        setMode(RenderingMode::WIREFRAME_AND_SURFACE);
    }
    ;
    BasicSCProperties(PhysicalModel *p, const std::string n) : Properties(p,n)  {
        alloc();
        setColor(DEFAULT);
        setMode(RenderingMode::WIREFRAME_AND_SURFACE);
    };

    virtual ~BasicSCProperties() {
        delete colorRGBA;
    };

    double getRed() const;
    double getGreen() const;
    double getBlue() const;
    double getAlpha() const;
    double * getRGB() const;
    double * getRGBA() const;
    Color getColor() const;

    void setRed(const double r);
    void setGreen(const double g);
    void setBlue(const double b);
    void setAlpha(const double a);
    void setRGB(const double * rgb);
    void setRGB(const double r, const double g, const double b);
    void setRGBA(const double r, const double g, const double b, const double a);
    void setRGBA(const double * rgba);
    void setColor(Color c);

    void setMode(const RenderingMode::Mode);
    RenderingMode::Mode getMode() const;
    bool isVisible(const RenderingMode::Mode mode) const;
    void setVisible(const RenderingMode::Mode, const bool);

    /// get the string equivalent to the enum rendering mode
    std::string getModeString() const;

    /** print to an output stream in "pseaudo" XML format.
      * This method is to be implemented in the subclass (StructuralComponentProperties).
      * It HAS to call the beginXML() at the beginning and the endXML() at the end.
      */
    virtual void xmlPrint(std::ostream &) =0;

protected:
    /// write the default xml properties (beginning)
    void beginXML(std::ostream &);
    /// write the default xml properties (end)
    void endXML(std::ostream &);

private:
    double *colorRGBA;
    void alloc();
    Color color;
    RenderingMode mode;
};


inline BasicSCProperties::BasicSCProperties(PhysicalModel * p, xmlNodePtr node) :Properties(p)
{
	alloc();

	//search the name attribute
	xmlChar *pname = xmlGetProp(node, (const xmlChar*) "name");
	if(pname)
		setName((char*)pname);

	//get the pointer on color
	xmlNodePtr SCchild = node->xmlChildrenNode;
	while (SCchild && xmlStrcmp(SCchild->name,(const xmlChar*)"color")) SCchild= SCchild->next;
	if (SCchild) {
		//search the color attributes
		xmlChar *pr = xmlGetProp(SCchild, (const xmlChar*) "r");
		xmlChar *pg = xmlGetProp(SCchild, (const xmlChar*) "g");
		xmlChar *pb = xmlGetProp(SCchild, (const xmlChar*) "b");
		xmlChar *pa = xmlGetProp(SCchild, (const xmlChar*) "a");
		if (pr && pg && pb){
			setRGB(atof((char*)pr),atof((char*)pg),atof((char*)pb));
			if (pa)
				setAlpha(atof((char*)pa));
			else
				setAlpha(1.0);
		}
		else setColor(DEFAULT);
	}
	else setColor(DEFAULT);

	//search the renderingmode attribute
	xmlChar *pmode = xmlGetProp(node, (const xmlChar*) "mode");
	if(pmode){
		if(!xmlStrcmp(pmode, (const xmlChar*)"NONE")) setMode(RenderingMode::NONE);
		if(!xmlStrcmp(pmode, (const xmlChar*)"POINTS")) setMode(RenderingMode::POINTS);
		if(!xmlStrcmp(pmode, (const xmlChar*)"POINTS_AND_SURFACE")) setMode(RenderingMode::POINTS_AND_SURFACE);
		if(!xmlStrcmp(pmode, (const xmlChar*)"SURFACE")) setMode(RenderingMode::SURFACE);
		if(!xmlStrcmp(pmode, (const xmlChar*)"WIREFRAME_AND_SURFACE")) setMode(RenderingMode::WIREFRAME_AND_SURFACE);
		if(!xmlStrcmp(pmode, (const xmlChar*)"WIREFRAME_AND_POINTS")) setMode(RenderingMode::WIREFRAME_AND_POINTS);
		if(!xmlStrcmp(pmode, (const xmlChar*)"WIREFRAME")) setMode(RenderingMode::WIREFRAME);
		if(!xmlStrcmp(pmode, (const xmlChar*)"WIREFRAME_AND_SURFACE_AND_POINTS")) setMode(RenderingMode::WIREFRAME_AND_SURFACE_AND_POINTS);
	}
	else setMode(RenderingMode::NONE);

	
	//search the unknown attributes to fill the property fields map
	xmlAttr * attrs = node->properties;
	xmlNodePtr unknownAttrs = xmlNewNode(NULL, (xmlChar*)("unknownAttrs"));;
	while (attrs)
	{
		const xmlChar * pname = attrs->name;
		xmlChar * pval = attrs->children->content;
				
		if (pname && xmlStrcmp(pname, (xmlChar*)"name") && 
					 xmlStrcmp(pname, (xmlChar*)"index") &&
					 xmlStrcmp(pname, (xmlChar*)"mode")){
			xmlSetProp(unknownAttrs, pname, pval);
		}

		attrs = attrs->next;
	}

	//transform the unknown attributes to a property field map
	domToFields(unknownAttrs);
}


// inlines
inline 	double BasicSCProperties::getRed() const {
    return colorRGBA[0];
}
inline double BasicSCProperties::getGreen() const {
    return colorRGBA[1];
}
inline double BasicSCProperties::getBlue() const {
    return colorRGBA[2];
}
inline double BasicSCProperties::getAlpha() const {
    return colorRGBA[3];
}
inline double * BasicSCProperties::getRGB() const {
    return colorRGBA;
}
inline double * BasicSCProperties::getRGBA() const {
    return colorRGBA;
}
inline BasicSCProperties::Color BasicSCProperties::getColor() const {
    return color;
}
// TODO : a test for the color, if a composant is changed it might still be something we now
inline void BasicSCProperties::setRed(const double r) {
    colorRGBA[0]  = r;
    color = OTHER;
}
inline void BasicSCProperties::setGreen(const double g)  {
    colorRGBA[1] = g;
    color = OTHER;
}
inline void BasicSCProperties::setBlue(const double b)  {
    colorRGBA[2] = b;
    color = OTHER;
}
inline void BasicSCProperties::setAlpha(const double a)  {
    colorRGBA[3] = a;
    color = OTHER;
}
inline void BasicSCProperties::setRGB(const double r, const double g, const double b)  {
    setRed(r);
    setGreen(g);
    setBlue(b);
    color = OTHER;
}
inline void BasicSCProperties::setRGB(const double * rgb) {
    setRGB(rgb[0], rgb[1], rgb[2]);
    color = OTHER;
}
inline void BasicSCProperties::setRGBA(const double r, const double g, const double b, const double a)   {
    setRed(r);
    setGreen(g);
    setBlue(b);
    setAlpha(a);
    color = OTHER;
}
inline void BasicSCProperties::setRGBA(const double *rgba) {
    setRGBA(rgba[0], rgba[1], rgba[2], rgba[3]);
    color = OTHER;
}
inline void BasicSCProperties::setColor(Color c) {
    switch (c) {
    case RED:
        setRGBA(1.0, 0.0, 0.0, 1.0);
        break;
    case GREEN:
        setRGBA(0.0, 1.0, 0.0, 1.0);
        break;
    case BLUE:
        setRGBA(0.0, 0.0, 1.0, 1.0);
        break;
    default: // DEFAULT:or GRAY:or OTHER
        setRGBA(0.8, 0.8, 0.8, 1.0);
        break;
    }
    color = c;
}
inline void BasicSCProperties::alloc() {
    colorRGBA = new double[4];
}
inline void BasicSCProperties::setMode(const RenderingMode::Mode m) {
    mode.setMode(m);
}
inline RenderingMode::Mode BasicSCProperties::getMode() const {
    return mode.getMode();
}
inline std::string BasicSCProperties::getModeString() const {
    return mode.getModeString();
}
inline	bool BasicSCProperties::isVisible(const RenderingMode::Mode m) const {
    return mode.isVisible(m);
}
inline	void BasicSCProperties::setVisible(const RenderingMode::Mode m, const bool b) {
    this->mode.setVisible(m, b);
}

// write the default xml properties (beginning)
inline void BasicSCProperties::beginXML(std::ostream & o) {
    // print the name if there is one
    if (getName() != "")
        o<< " name=\"" << getName().c_str() << "\" ";

    // the mode property (if different than default)
    if (mode.getMode() != RenderingMode::NONE) {
        o << " mode=\"" << mode.getModeString() << "\" ";
    }
}

// write the default xml properties (end)
inline void BasicSCProperties::endXML(std::ostream &) {
    // nothing to be done
}


#endif