@@ -35,18 +35,119 @@ using namespace SCIRun::Core::Datatypes;
3535using 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
4040MODULE_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
4949void
5050CalculateNormals::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}
0 commit comments