Visualization Library 2.1.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
VolumePlot.cpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #include <vlVolume/VolumePlot.hpp>
33 #include <vlGraphics/Light.hpp>
38 
39 using namespace vl;
40 
69 //-----------------------------------------------------------------------------
70 // VolumePlot
71 //-----------------------------------------------------------------------------
73 {
74  VL_DEBUG_SET_OBJECT_NAME()
77  mIsosurfaceActor = new Actor;
80  mBoxEffect = new Effect;
81  mTextTemplate = new Text;
82  mSamplingResolution = ivec3(64,64,64);
83  mLabelFormat = "(%.2n %.2n %.2n)";
84  mLabelFont = defFontManager()->acquireFont("/font/bitstream-vera/VeraMono.ttf", 8);
85  mMinCorner = fvec3(-1,-1,-1);
86  mMaxCorner = fvec3(+1,+1,+1);
87 
88  // defaults
89 
90  mIsosurfaceEffect->shader()->setRenderState( new Light, 0 );
91  mIsosurfaceEffect->shader()->gocMaterial()->setFrontDiffuse(red);
92  mIsosurfaceEffect->shader()->gocMaterial()->setBackDiffuse(green);
93  mIsosurfaceEffect->shader()->enable(EN_LIGHTING);
94  mIsosurfaceEffect->shader()->enable(EN_DEPTH_TEST);
95  mIsosurfaceEffect->shader()->gocLightModel()->setTwoSide(true);
96 
97  mBoxEffect->shader()->gocPolygonMode()->set(PM_LINE, PM_LINE);
98  mBoxEffect->shader()->enable(EN_DEPTH_TEST);
99 
100  textTemplate()->setColor(white);
101 }
102 //-----------------------------------------------------------------------------
107 void VolumePlot::compute(const Function& func, float threshold)
108 {
109  actorTreeMulti()->actors()->clear();
110  mActors.clear();
111 
112  // volume_box outline
113  ref<Geometry> box_outline = makeBox(AABB((vec3)minCorner(),(vec3)maxCorner()));
114 
115  // setup isosurface and actors
116 
117  MarchingCubes mc;
118 
119  mIsosurfaceGeometry->setVertexArray(mc.mVertsArray.get());
120  mIsosurfaceGeometry->setNormalArray(mc.mNormsArray.get());
121  mIsosurfaceGeometry->drawCalls().clear();
122  mIsosurfaceGeometry->drawCalls().push_back(mc.mDrawElements.get());
123 
127  mActors.push_back(mIsosurfaceActor.get());
128  mActors.push_back( new Actor(box_outline.get(),mBoxEffect.get(),mPlotTransform.get()) );
129 
130  ref<Volume> volume = new Volume;
131  volume->setup( NULL, false, false, minCorner(), maxCorner(), mSamplingResolution );
132 
133  mc.volumeInfo()->push_back( new VolumeInfo( volume.get(), threshold ) );
134 
135  evaluateFunction(volume->values(), minCorner(), maxCorner(), func);
136 
137  // generate vertices and polygons
138  mc.run(false);
139 
140  // setup labels
142 
143  for(unsigned i=0; i<mActors.size(); ++i)
144  actorTreeMulti()->actors()->push_back(mActors[i].get());
145 }
146 //-----------------------------------------------------------------------------
147 void VolumePlot::setupLabels(const String& format, const fvec3& min_corner, const fvec3& max_corner, Font* font, Transform* root_tr)
148 {
149  ref< Effect > text_fx = new Effect;
150  text_fx->shader()->enable(EN_BLEND);
151  text_fx->shader()->enable(EN_DEPTH_TEST);
152 
153  float coords[][3] =
154  {
155  {min_corner.x(), min_corner.y(), min_corner.z()},
156  {max_corner.x(), min_corner.y(), min_corner.z()},
157  {max_corner.x(), max_corner.y(), min_corner.z()},
158  {min_corner.x(), max_corner.y(), min_corner.z()},
159  {min_corner.x(), min_corner.y(), max_corner.z()},
160  {max_corner.x(), min_corner.y(), max_corner.z()},
161  {max_corner.x(), max_corner.y(), max_corner.z()},
162  {min_corner.x(), max_corner.y(), max_corner.z()}
163  };
164 
165  String coord_label[] =
166  {
167  Say(format) << min_corner.x() << min_corner.y() << min_corner.z(),
168  Say(format) << max_corner.x() << min_corner.y() << min_corner.z(),
169  Say(format) << max_corner.x() << max_corner.y() << min_corner.z(),
170  Say(format) << min_corner.x() << max_corner.y() << min_corner.z(),
171  Say(format) << min_corner.x() << min_corner.y() << max_corner.z(),
172  Say(format) << max_corner.x() << min_corner.y() << max_corner.z(),
173  Say(format) << max_corner.x() << max_corner.y() << max_corner.z(),
174  Say(format) << min_corner.x() << max_corner.y() << max_corner.z()
175  };
176 
177  for(int i=0; i<8; ++i)
178  {
179  ref<Text> text = new Text;
180  text->setDisplayListEnabled(false);
181  text->setBufferObjectEnabled(false);
182  text->setFont( font );
184  text->setText(coord_label[i]);
185  // template parameters
186  text->setColor( textTemplate()->color() );
187  text->setBorderColor( textTemplate()->borderColor() );
188  text->setBorderEnabled( textTemplate()->borderEnabled() );
189  text->setBackgroundColor( textTemplate()->backgroundColor() );
190  text->setBackgroundEnabled( textTemplate()->backgroundEnabled() );
191  text->setMargin( textTemplate()->margin() );
192  text->setOutlineColor( textTemplate()->outlineColor() );
193  text->setOutlineEnabled( textTemplate()->outlineEnabled() );
194  text->setShadowColor( textTemplate()->shadowColor() );
195  text->setShadowEnabled( textTemplate()->shadowEnabled() );
196  text->setShadowVector( textTemplate()->shadowVector() );
197  text->setMatrix( textTemplate()->matrix() );
198 
199  ref<Actor> text_a = new Actor( text.get(), text_fx.get(), new Transform );
200  text_a->transform()->setLocalMatrix( mat4::getTranslation(coords[i][0],coords[i][1],coords[i][2]) );
201  if (root_tr)
202  root_tr->addChild( text_a->transform() );
203  text_a->transform()->computeWorldMatrix();
204 
205  mActors.push_back( text_a.get() );
206  }
207 }
208 //-----------------------------------------------------------------------------
209 void VolumePlot::evaluateFunction(float* scalar, const fvec3& min_corner, const fvec3& max_corner, const Function& func)
210 {
211  fvec3 v;
212  int w = mSamplingResolution.x();
213  int h = mSamplingResolution.y();
214  int d = mSamplingResolution.z();
215  for(int z=0; z<d; ++z)
216  {
217  float tz = (float)z/(d-1);
218  v.z() = min_corner.z()*(1.0f-tz) + max_corner.z()*tz;
219  for(int y=0; y<h; ++y)
220  {
221  float ty = (float)y/(h-1);
222  v.y() = min_corner.y()*(1.0f-ty) + max_corner.y()*ty;
223  for(int x=0; x<w; ++x)
224  {
225  float tx = (float)x/(w-1);
226  v.x() = min_corner.x()*(1.0f-tx) + max_corner.x()*tx;
227  // evaluate function
228  scalar[x+y*w+z*w*h] = func(v.x(),v.y(),v.z());
229  }
230  }
231  }
232 }
233 //-----------------------------------------------------------------------------
void setupLabels(const String &format, const fvec3 &min_corner, const fvec3 &max_corner, Font *font, Transform *root_tr)
Definition: VolumePlot.cpp:147
Associates a Renderable object to an Effect and Transform.
Definition: Actor.hpp:130
Defines the volume data to be used with a MarchingCube object.
void setEffect(Effect *effect)
Binds an Effect to an Actor.
Definition: Actor.hpp:196
ref< Text > mTextTemplate
Definition: VolumePlot.hpp:147
ref< DrawElementsUShort > mDrawElements
Vector3< float > fvec3
A 3 components vector with float precision.
Definition: Vector3.hpp:253
Transform * transform()
Returns the Transform bound tho an Actor.
Definition: Actor.hpp:190
Implements a 4x4 matrix transform used to define the position and orientation of an Actor...
Definition: Transform.hpp:72
void setShadowColor(const fvec4 &shadow_color)
Definition: Text.hpp:78
const T * get() const
Definition: Object.hpp:128
ref< Effect > mIsosurfaceEffect
Definition: VolumePlot.hpp:145
If enabled, do depth comparisons and update the depth buffer; Note that even if the depth buffer exis...
String mLabelFormat
Definition: VolumePlot.hpp:139
void setup(float *data, bool use_directly, bool copy_data, const fvec3 &bottom_left, const fvec3 &top_right, const ivec3 &slices)
Setup the volume data with the specified memory management.
const fvec3 & minCorner() const
Default value: fvec3(-1,-1,-1)
Definition: VolumePlot.hpp:90
A simple String formatting class.
Definition: Say.hpp:124
A Renderable that renders text with a given Font.
Definition: Text.hpp:50
void setLocalMatrix(const mat4 &m)
The matrix representing the transform&#39;s local space.
Definition: Transform.hpp:139
ActorTree * actorTreeMulti()
Definition: VolumePlot.hpp:128
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
Definition: String.hpp:62
const String & labelFormat() const
Sets the format of the labels.
Definition: VolumePlot.hpp:112
const T_Scalar & z() const
Definition: Vector3.hpp:92
A font to be used with a Text renderable.
Definition: Font.hpp:127
void run(bool generate_colors)
If enabled, use the current lighting parameters to compute the vertex color; Otherwise, simply associate the current color with each vertex, see also Material, LightModel, and Light.
void setText(const String &text)
Definition: Text.hpp:63
If enabled, blend the incoming RGBA color values with the values in the color buffers, see also BlendFunc for more information.
Font * acquireFont(const String &font, int size, bool smooth=false)
Creates or returns an already created Font.
Definition: FontManager.cpp:68
ref< Actor > mIsosurfaceActor
Definition: VolumePlot.hpp:144
Defines the volume parameters to be used with a MarchingCube and Volume object.
void setFont(Font *font)
Definition: Text.hpp:88
void setBorderEnabled(bool border)
Definition: Text.hpp:112
const Collection< VolumeInfo > * volumeInfo() const
The Geometry class is a Renderable that implements a polygonal mesh made of polygons, lines and points.
Definition: Geometry.hpp:66
An efficient implementation of the Marching Cubes algorithm.
Visualization Library main namespace.
ref< ArrayFloat3 > mVertsArray
VLGRAPHICS_EXPORT ref< Geometry > makeBox(const vec3 &origin, real xside=1, real yside=1, real zside=1, bool tex_coords=true)
Creates a box.
void evaluateFunction(float *scalar, const fvec3 &min_corner, const fvec3 &max_corner, const Function &func)
Definition: VolumePlot.cpp:209
void setBackgroundColor(const fvec4 &background_color)
Definition: Text.hpp:75
const ActorCollection * actors() const
Returns the actors contained in a ActorTree node.
void setOutlineColor(const fvec4 &outline_color)
Definition: Text.hpp:72
The AABB class implements an axis-aligned bounding box using vl::real precision.
Definition: AABB.hpp:44
ref< Transform > mPlotTransform
Definition: VolumePlot.hpp:137
ref< ArrayFloat3 > mNormsArray
std::vector< ref< Actor > > mActors
Definition: VolumePlot.hpp:136
virtual void computeWorldMatrix(Camera *=NULL)
Computes the world matrix by concatenating the parent&#39;s world matrix with its own local matrix...
Definition: Transform.hpp:195
A function to be used with VolumePlot.
Definition: VolumePlot.hpp:56
const float * values() const
void setShadowVector(const fvec2 &shadow_vector)
Definition: Text.hpp:81
void push_back(T *data)
Definition: Collection.hpp:79
const T_Scalar & y() const
Definition: Vector3.hpp:91
void setTransform(Transform *transform)
Binds a Transform to an Actor.
Definition: Actor.hpp:182
ref< Effect > mBoxEffect
Definition: VolumePlot.hpp:146
ref< Font > mLabelFont
Definition: VolumePlot.hpp:140
#define NULL
Definition: OpenGLDefs.hpp:81
ivec3 mSamplingResolution
Definition: VolumePlot.hpp:138
Vector3< int > ivec3
A 3 components vector with int precision.
Definition: Vector3.hpp:249
Shader * shader(int lodi=0, int pass=0)
Utility function, same as &#39;lod(lodi)->at(pass);&#39;.
Definition: Effect.hpp:178
Defines the sequence of Shader objects used to render an Actor.
Definition: Effect.hpp:91
void setColor(const fvec4 &color)
Definition: Text.hpp:66
void setDisplayListEnabled(bool enabled)
Enable/disable display lists (disabled by default).
Definition: Renderable.hpp:196
void setShadowEnabled(bool shadow)
Definition: Text.hpp:124
void setMargin(int margin)
Definition: Text.hpp:84
static Matrix4 & getTranslation(Matrix4 &out, const Vector3< float > &v)
Definition: Matrix4.hpp:553
void setLod(int lod_index, Renderable *renderable)
Sets the Renderable object representing the LOD level specifed by lod_index.
Definition: Actor.hpp:159
const fvec3 & maxCorner() const
Default value: fvec3(+1,+1,+1)
Definition: VolumePlot.hpp:95
Wraps the OpenGL function glLight().
Definition: Light.hpp:51
const T_Scalar & x() const
Definition: Vector3.hpp:90
The ref<> class is used to reference-count an Object.
Definition: Object.hpp:55
void setBorderColor(const fvec4 &border_color)
Definition: Text.hpp:69
The ActorTree class implements a generic tree whose nodes contain Actors.
Definition: ActorTree.hpp:62
void setBufferObjectEnabled(bool enabled)
Enable/disable BufferObject (vertex buffer object) (enabled by default).
Definition: Renderable.hpp:208
void setAlignment(int align)
Definition: Text.hpp:94
void enable(EEnable capability)
Definition: Shader.hpp:2158
void setMatrix(const fmat4 &matrix)
Definition: Text.hpp:91
const Text * textTemplate() const
A Text used to initialize the plot labels.
Definition: VolumePlot.hpp:124
const Font * labelFont() const
The Font to be used for the box labels.
Definition: VolumePlot.hpp:117
void compute(const Function &func, float threshold)
Computes the function and generates the plot. This method should be called after all the other method...
Definition: VolumePlot.cpp:107
void addChild(Transform *child)
Adds a child transform.
Definition: Transform.hpp:246
VLGRAPHICS_EXPORT FontManager * defFontManager()
Returns the default FontManager used by Visualization Library.
Definition: pimpl.cpp:43
void setBackgroundEnabled(bool background)
Definition: Text.hpp:115
ref< Geometry > mIsosurfaceGeometry
Definition: VolumePlot.hpp:143
VolumePlot()
Constructor.
Definition: VolumePlot.cpp:72
void setOutlineEnabled(bool outline)
Definition: Text.hpp:121
ref< ActorTree > mActorTreeMulti
Definition: VolumePlot.hpp:148