SampleF.cc 6.51 KB
Newer Older
Will Schroeder's avatar
Will Schroeder committed
1
2
3
4
5
/*=========================================================================

  Program:   Visualization Library
  Module:    SampleF.cc
  Language:  C++
Will Schroeder's avatar
Will Schroeder committed
6
7
  Date:      $Date$
  Version:   $Revision$
Will Schroeder's avatar
Will Schroeder committed
8
9
10
11
12
13
14
15
16
17
18

This file is part of the Visualization Library. No part of this file
or its contents may be copied, reproduced or altered in any way
without the express written consent of the authors.

Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen 1993, 1994 

=========================================================================*/
#include <math.h>
#include "SampleF.hh"
#include "FScalars.hh"
Will Schroeder's avatar
Will Schroeder committed
19
#include "FNormals.hh"
Will Schroeder's avatar
Will Schroeder committed
20

Will Schroeder's avatar
Will Schroeder committed
21
// Description:
Will Schroeder's avatar
Will Schroeder committed
22
23
// Construct with ModelBounds=(-1,1,-1,1,-1,1), SampleDimensions=(50,50,50),
// Capping turned off, and normal generation on.
Will Schroeder's avatar
Will Schroeder committed
24
25
26
27
28
29
30
31
32
33
34
35
36
vlSampleFunction::vlSampleFunction()
{
  this->ModelBounds[0] = -1.0;
  this->ModelBounds[1] = 1.0;
  this->ModelBounds[2] = -1.0;
  this->ModelBounds[3] = 1.0;
  this->ModelBounds[4] = -1.0;
  this->ModelBounds[5] = 1.0;

  this->SampleDimensions[0] = 50;
  this->SampleDimensions[1] = 50;
  this->SampleDimensions[2] = 50;

Will Schroeder's avatar
Will Schroeder committed
37
  this->Capping = 0;
Will Schroeder's avatar
Will Schroeder committed
38
39
40
  this->CapValue = LARGE_FLOAT;

  this->ImplicitFunction = NULL;
Will Schroeder's avatar
Will Schroeder committed
41
42

  this->ComputeNormals = 1;
Will Schroeder's avatar
Will Schroeder committed
43
44
45
46
47
48
}

void vlSampleFunction::PrintSelf(ostream& os, vlIndent indent)
{
  if (this->ShouldIPrint(vlSampleFunction::GetClassName()))
    {
Will Schroeder's avatar
Will Schroeder committed
49
    vlStructuredPointsSource::PrintSelf(os,indent);
Will Schroeder's avatar
Will Schroeder committed
50
51
52
53
54
55
56
57
58
59
60

    os << indent << "Sample Dimensions: (" << this->SampleDimensions[0] << ", "
                 << this->SampleDimensions[1] << ", "
                 << this->SampleDimensions[2] << ")\n";
    os << indent << "ModelBounds: \n";
    os << indent << "  Xmin,Xmax: (" << this->ModelBounds[0] << ", " << this->ModelBounds[1] << ")\n";
    os << indent << "  Ymin,Ymax: (" << this->ModelBounds[2] << ", " << this->ModelBounds[3] << ")\n";
    os << indent << "  Zmin,Zmax: (" << this->ModelBounds[4] << ", " << this->ModelBounds[5] << ")\n";
    }
}

Will Schroeder's avatar
Will Schroeder committed
61
62
63
// Description:
// Specify the model bounds is the location in space in which the 
// sampling occurs.
Will Schroeder's avatar
Will Schroeder committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
void vlSampleFunction::SetModelBounds(float xmin, float xmax, float ymin, float ymax, float zmin, float zmax)
{
  if (this->ModelBounds[0] != xmin || this->ModelBounds[1] != xmax ||
  this->ModelBounds[2] != ymin || this->ModelBounds[3] != ymax ||
  this->ModelBounds[4] != zmin || this->ModelBounds[5] != zmax )
    {
    float length;

    this->Modified();
    this->ModelBounds[0] = xmin;
    this->ModelBounds[1] = xmax;
    this->ModelBounds[2] = ymin;
    this->ModelBounds[3] = ymax;
    this->ModelBounds[4] = zmin;
    this->ModelBounds[5] = zmax;
    }
}

Will Schroeder's avatar
Will Schroeder committed
82
83
84
85
86
void vlSampleFunction::SetModelBounds(float *bounds)
{
  vlSampleFunction::SetModelBounds(bounds[0], bounds[1], bounds[2], bounds[3], bounds[4], bounds[5]);
}

Will Schroeder's avatar
Will Schroeder committed
87
88
void vlSampleFunction::Execute()
{
Will Schroeder's avatar
Will Schroeder committed
89
  int ptId, i;
Will Schroeder's avatar
Will Schroeder committed
90
  vlFloatScalars *newScalars;
Will Schroeder's avatar
Will Schroeder committed
91
  vlFloatNormals *newNormals=NULL;
Will Schroeder's avatar
Will Schroeder committed
92
  int numPts;
Will Schroeder's avatar
Will Schroeder committed
93
  float *p, s;
Will Schroeder's avatar
Will Schroeder committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110

  vlDebugMacro(<< "Sampling implicit function");
//
// Initialize self; create output objects
//
  this->Initialize();

  if ( !this->ImplicitFunction )
    {
    vlErrorMacro(<<"No implicit function specified");
    return;
    }

  numPts = this->SampleDimensions[0] * this->SampleDimensions[1] 
           * this->SampleDimensions[2];
  newScalars = new vlFloatScalars(numPts);

Will Schroeder's avatar
Will Schroeder committed
111
  // Compute origin and aspect ratio
Will Schroeder's avatar
Will Schroeder committed
112
  this->SetDimensions(this->GetSampleDimensions());
Will Schroeder's avatar
Will Schroeder committed
113
114
115
116
117
118
  for (i=0; i < 3; i++)
    {
    this->Origin[i] = this->ModelBounds[2*i];
    this->AspectRatio[i] = (this->ModelBounds[2*i+1] - this->ModelBounds[2*i])
                           / (this->SampleDimensions[i] - 1);
    }
Will Schroeder's avatar
Will Schroeder committed
119
120
121
122
123
//
// Traverse all points evaluating implicit function at each point
//
  for (ptId=0; ptId < numPts; ptId++ )
    {
Will Schroeder's avatar
Will Schroeder committed
124
    p = this->GetPoint(ptId);
Will Schroeder's avatar
Will Schroeder committed
125
126
127
128
    s = this->ImplicitFunction->Evaluate(p[0], p[1], p[2]);
    newScalars->SetScalar(ptId,s);
    }
//
Will Schroeder's avatar
Will Schroeder committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// If normal computation turned on, compute them
//
  if ( this->ComputeNormals )
    {
    float n[3];
    newNormals = new vlFloatNormals(numPts);
    for (ptId=0; ptId < numPts; ptId++ )
      {
      p = this->GetPoint(ptId);
      this->ImplicitFunction->EvaluateNormal(p[0], p[1], p[2], n);
      newNormals->SetNormal(ptId,n);
      }
    }
//
Will Schroeder's avatar
Will Schroeder committed
143
144
145
146
147
148
149
150
151
152
153
// If capping is turned on, set the distances of the outside of the volume
// to the CapValue.
//
  if ( this->Capping )
    {
    this->Cap(newScalars);
    }
//
// Update self
//
  this->PointData.SetScalars(newScalars);
Will Schroeder's avatar
Will Schroeder committed
154
  this->PointData.SetNormals(newNormals);
Will Schroeder's avatar
Will Schroeder committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
}


void vlSampleFunction::SetSampleDimensions(int i, int j, int k)
{
  int dim[3];

  dim[0] = i;
  dim[1] = j;
  dim[2] = k;

  this->SetSampleDimensions(dim);
}

void vlSampleFunction::SetSampleDimensions(int dim[3])
{
  vlDebugMacro(<< " setting SampleDimensions to (" << dim[0] << "," << dim[1] << "," << dim[2] << ")");

  if ( dim[0] != this->SampleDimensions[0] || dim[1] != SampleDimensions[1] ||
  dim[2] != SampleDimensions[2] )
    {
Will Schroeder's avatar
Will Schroeder committed
176
177
    for ( int i=0; i<3; i++) 
      this->SampleDimensions[i] = (dim[i] > 0 ? dim[i] : 1);
Will Schroeder's avatar
Will Schroeder committed
178
179
180
181
    this->Modified();
    }
}

Will Schroeder's avatar
Will Schroeder committed
182
183
184
185
186
187
188
189
190
191
192
193
194
195
unsigned long vlSampleFunction::GetMTime()
{
  unsigned long mTime=this->MTime.GetMTime();
  unsigned long impFuncMTime;

  if ( this->ImplicitFunction != NULL )
    {
    impFuncMTime = this->ImplicitFunction->GetMTime();
    mTime = ( impFuncMTime > mTime ? impFuncMTime : mTime );
    }

  return mTime;
}

Will Schroeder's avatar
Will Schroeder committed
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
void vlSampleFunction::Cap(vlFloatScalars *s)
{
  int i,j,k;
  int idx;
  int d01=this->SampleDimensions[0]*this->SampleDimensions[1];

// i-j planes
  k = 0;
  for (j=0; j<this->SampleDimensions[1]; j++)
    for (i=0; i<this->SampleDimensions[0]; i++)
      s->SetScalar(i+j*this->SampleDimensions[1], this->CapValue);

  k = this->SampleDimensions[2] - 1;
  idx = k*d01;
  for (j=0; j<this->SampleDimensions[1]; j++)
    for (i=0; i<this->SampleDimensions[0]; i++)
      s->SetScalar(idx+i+j*this->SampleDimensions[1], this->CapValue);

// j-k planes
  i = 0;
  for (k=0; k<this->SampleDimensions[2]; k++)
    for (j=0; j<this->SampleDimensions[1]; j++)
      s->SetScalar(j*this->SampleDimensions[0]+k*d01, this->CapValue);

  i = this->SampleDimensions[0] - 1;
  for (k=0; k<this->SampleDimensions[2]; k++)
    for (j=0; j<this->SampleDimensions[1]; j++)
      s->SetScalar(i+j*this->SampleDimensions[0]+k*d01, this->CapValue);

// i-k planes
  j = 0;
  for (k=0; k<this->SampleDimensions[2]; k++)
    for (i=0; i<this->SampleDimensions[0]; i++)
      s->SetScalar(i+k*d01, this->CapValue);

  j = this->SampleDimensions[1] - 1;
  idx = j*this->SampleDimensions[0];
  for (k=0; k<this->SampleDimensions[2]; k++)
    for (i=0; i<this->SampleDimensions[0]; i++)
      s->SetScalar(idx+i+k*d01, this->CapValue);

}