File size: 5,205 Bytes
ae4627d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Eisung Sohn : essohn@gmail.com

//-----------------------------------------------------------------------------------

function SDCSConstraint (node) {
  
  this.node = node;
  this.focussed = false;
  this.active = false;

  this.init_position = new THREE.Vector3();
  this.init_position.set(node.init_pos.x, node.init_pos.y, node.init_pos.z);
  
  this.position = new THREE.Vector3();
  this.position.set(node.k_pos.x, node.k_pos.y, node.k_pos.z);

  this.fixed = true;
  this.rotation = new THREE.Vector3();
  
  this.pos_weight = 10.0;
  this.rot_weight = 10.0;
  
  this.render_obj = null;
  
}

function SDCSConstraint_manager () {
  
}

SDCSConstraint_manager.prototype = {
  
  init: function (scene_manager) {
  
    this.scene_manager = scene_manager;
    this.scene = scene_manager.scene_selectable;
    this.model = scene_manager.model;
  
  },
  
  load: function (init_pos, rotation, fixed, pos_weight, rot_weight) {
    var min_length = 10000000;
    var min_index = -1;
    for (var i=0; i< this.model.sdcs_data.cnodes.length; i++) {
      var node = this.model.sdcs_data.cnodes[i];
      var len = (new THREE.Vector3( node.init_pos.x - init_pos.x, node.init_pos.y - init_pos.y, node.init_pos.z - init_pos.z)).length();
      if (len < min_length) {
        min_index = i;
        min_length = len;
      }
    }
    
    if (min_index >= 0) {
      var c = this.create( this.model.sdcs_data.cnodes[min_index], fixed );
      c.rotation.set( rotation.x, rotation.y, rotation.z );
      c.pos_weight = pos_weight;
      c.rot_weight = rot_weight;
    }
    
  },
  
  add_helper: function (constraint) {
    
    if ( constraint.render_object_circle ) {
      constraint.render_object.remove( constraint.render_object_circle );
      constraint.render_object_circle = null;
    }
    if ( constraint.render_object_cross ) {
      constraint.render_object.remove( constraint.render_object_cross );
      constraint.render_object_cross = null;
    }
    
    // add circle object
		var arcShape = new THREE.Shape();
		arcShape.absarc( 0, 0, constraint.pos_weight / 2.0, 0, Math.PI*2, false );
          
    var points = arcShape.createPointsGeometry();
    var circle = new THREE.Line( points, new THREE.LineBasicMaterial( { color: 0xffff00, transparent: true, opacity: 0.5 } ) );
    
    constraint.render_object.add( circle );
    constraint.render_object_circle = circle;
    
    // add cross object
		var cross_len = constraint.rot_weight;

		var crossShape = new THREE.Shape();
		crossShape.moveTo( 0, - cross_len/2 );
		crossShape.lineTo( 0, cross_len/2 );
		crossShape.moveTo( - cross_len/2, 0 );
		crossShape.lineTo( cross_len/2, 0 );
    
    var points = crossShape.createPointsGeometry();
    var cross = new THREE.Line( points, new THREE.LineBasicMaterial( { color: 0x00ff00, transparent: true, opacity: 0.5 } ) );
    
    constraint.render_object.add( cross );
    constraint.render_object_cross = cross;
    
  },
  
  create: function (node, fixed) {
  
    if (node) {
    
      var model = this.model;

      // check duplicate
      for (var i in model.sdcs_constraints) {
        if ( model.sdcs_constraints[i].node == node )
          return;
      }
    
      model.sdcs_deformer.precomputed = false;
    
      var constraint = new SDCSConstraint( node );
      
      if (fixed) {
        constraint.fixed = fixed;
        constraint.rot_weight = 10.0;
      } else {
        constraint.rot_weight = 0.0;
      }

      // add render object
      var geometry = new THREE.SphereGeometry( 1.0, 12, 12 );
      var material = new THREE.MeshBasicMaterial( {transparent: true, color: 0xffff00} );
  		material.color.setHSL( 0.15, 1.0, 0.5 );

      var render_obj = new THREE.Mesh( geometry, material );

      render_obj.position.set(constraint.position.x, constraint.position.y, constraint.position.z);

      // user data
      render_obj.class = this.scene_manager.object_type.CONSTRAINT;
      render_obj.ref_object = constraint;

      constraint.render_object = render_obj;

      this.add_helper(constraint);
      
      // add constraint
      this.model.sdcs_constraints.push(constraint);
  
      // add to scene
  		this.scene.add( render_obj );
  
      console.log(this.model.sdcs_constraints);
    
      return constraint;
    }
  
    return null;
  },
  
  delete: function (constraint) {

    if (constraint) {
    
      this.model.sdcs_deformer.precomputed = false;
    
      var index = -1;
      for (var i in this.model.sdcs_constraints) {
        if (this.model.sdcs_constraints[i] == constraint) {
          // remove from scene
      		this.scene.remove( this.model.sdcs_constraints[i].render_object );
        
          // remove from array
          this.model.sdcs_constraints.splice(i,1);
          break;
        }
      }
      console.log(this.model.sdcs_constraints);
    }
  
  },
  
  clear: function () {

    this.model.sdcs_deformer.precomputed = false;

    while( this.model.sdcs_constraints.length > 0) {
        // remove from scene
    		this.scene.remove( this.model.sdcs_constraints[0].render_object );
      
        // remove from array
        this.model.sdcs_constraints.splice(0,1);
    }
    
  }
  
  
}