Skip to content

Commit 023ae28

Browse files
committed
Add calculate node normals
1 parent 974d56f commit 023ae28

File tree

2 files changed

+106
-6
lines changed

2 files changed

+106
-6
lines changed

src/Modules/Legacy/Fields/CalculateNormals.cc

Lines changed: 104 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,119 @@ using namespace SCIRun::Core::Datatypes;
3535
using namespace SCIRun::Dataflow::Networks;
3636

3737
/// @class CalculateNormals
38-
/// @brief This module splits out a string.
38+
/// @brief Make a new vector field that points to the input point.
3939

4040
MODULE_INFO_DEF(CalculateNormals, ChangeFieldData, SCIRun) ;
4141

42-
CalculateNormals::CalculateNormals() : Module(staticInfo_)
42+
CalculateNormals::CalculateNormals() : Module(staticInfo_, false)
4343
{
4444
INITIALIZE_PORT(InputField);
45-
INITIALIZE_PORT(InputField2);
45+
INITIALIZE_PORT(InputPoint);
4646
INITIALIZE_PORT(OutputField);
4747
}
4848

4949
void
5050
CalculateNormals::execute()
5151
{
52+
auto ifieldhandle = getRequiredInput(InputField);
53+
auto ipointhandle = getRequiredInput(InputPoint);
54+
55+
if ( inputs_changed_ || !oport_cached("Output Field"))
56+
{
57+
update_state(Executing);
58+
59+
VField* point_field = ipointhandle->vfield();
60+
VMesh* point_mesh = ipointhandle->vmesh();
61+
62+
if (point_mesh->num_nodes() != 1)
63+
{
64+
error("Input Point needs to have a single node only");
65+
return;
66+
}
67+
68+
Point attract_point;
69+
Vector dir;
70+
bool has_vector = false;
71+
point_mesh->get_center(attract_point,VMesh::Node::index_type(0));
72+
73+
if (!(point_field->is_pointcloudmesh()))
74+
{
75+
error("Input field was not a valid point cloud.");
76+
return;
77+
}
78+
79+
if (point_field->is_vector())
80+
{
81+
has_vector = true;
82+
point_field->get_value(dir,0);
83+
}
84+
85+
VField* field = ifieldhandle->vfield();
86+
87+
VField::size_type num_values = field->num_values();
88+
89+
FieldInformation fi(ifieldhandle);
90+
if (fi.field_basis_order() < 1)
91+
{
92+
fi.make_lineardata();
93+
fi.make_vector();
94+
}
95+
96+
fi.make_vector();
97+
98+
auto ofieldhandle = CreateField(fi,ifieldhandle->mesh());
99+
VField* ofield = ofieldhandle->vfield();
100+
ofield->resize_values();
101+
102+
int cnt = 0;
103+
104+
if (field->is_scalar() && field->basis_order() == 1)
105+
{
106+
Point c; Vector vec; Vector diff; double val;
107+
for (VField::index_type idx = 0; idx<num_values;idx++)
108+
{
109+
field->get_center(c,idx);
110+
if (has_vector)
111+
{
112+
diff = c - attract_point;
113+
vec = (dir*Dot(dir,diff)-diff);
114+
vec.safe_normalize();
115+
}
116+
else
117+
{
118+
vec = (attract_point - c);
119+
vec.safe_normalize();
120+
}
121+
122+
field->get_value(val,idx);
123+
if (val == 0.0) val = 1e-3;
124+
vec = vec*val;
125+
ofield->set_value(vec,idx);
126+
cnt++; if (cnt == 400) { cnt=0; update_progress(idx,num_values); }
127+
}
128+
}
129+
else
130+
{
131+
Point c; Vector vec; Vector diff;
132+
for (VField::index_type idx = 0; idx<num_values;idx++)
133+
{
134+
field->get_center(c,idx);
135+
if (has_vector)
136+
{
137+
diff = c - attract_point;
138+
vec = (dir*Dot(dir,diff)-diff);
139+
vec.safe_normalize();
140+
}
141+
else
142+
{
143+
vec = (attract_point - c);
144+
vec.safe_normalize();
145+
}
146+
ofield->set_value(vec,idx);
147+
cnt++; if (cnt == 400) { cnt=0; update_progress(idx,num_values); }
148+
}
149+
}
150+
151+
sendOutput(OutputField, ofield);
152+
}
52153
}

src/Modules/Legacy/Fields/CalculateNormals.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,17 @@ namespace Fields {
4747
class SCISHARE CalculateNormals : public SCIRun::Dataflow::Networks::Module,
4848
public Has1OutputPort<FieldPortTag>,
4949
public Has2InputPorts<FieldPortTag, FieldPortTag>
50-
//public Has1OutputPort<FieldPortTag>
5150
{
5251
public:
5352
// these functions are required for all modules
5453
CalculateNormals();
54+
5555
virtual void execute();
5656
virtual void setStateDefaults(){}
5757

5858
//name the ports and datatype.
5959
INPUT_PORT(0, InputField, Field);
60-
INPUT_PORT(1, InputField2, Field);
61-
// OUTPUT_PORT(0, OutputField, Field);
60+
INPUT_PORT(1, InputPoint, Field);
6261
OUTPUT_PORT(0, OutputField, Field);
6362

6463
// this is needed for the module factory

0 commit comments

Comments
 (0)