]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/object.cpp
1b48a862ae6b634c499fa49c094e54bc5c41cb05
[r2c2.git] / source / libr2c2 / object.cpp
1 #include <stdexcept>
2 #include "object.h"
3
4 using namespace std;
5
6 namespace R2C2 {
7
8 Snap Object::get_snap_node(unsigned) const
9 {
10         throw out_of_range("Object::get_snap_node");
11 }
12
13 bool Object::snap(Snap &sn, float limit, SnapType what) const
14 {
15         if(what&SNAP_NODE)
16         {
17                 unsigned nsn = get_n_snap_nodes();
18                 for(unsigned i=0; i<nsn; ++i)
19                 {
20                         Snap node = get_snap_node(i);
21                         Vector d(sn.position.x-node.position.x, sn.position.y-node.position.y, sn.position.z-node.position.z);
22                         if(d.x*d.x+d.y*d.y<limit*limit)
23                         {
24                                 sn = node;
25                                 return true;
26                         }
27                 }
28         }
29
30         return false;
31 }
32
33 bool Object::snap_to(const Object &other, float limit, SnapType what)
34 {
35         if(what==SNAP_DEFAULT)
36                 what = get_default_snap_type_to(other);
37
38         if(!what)
39                 return false;
40
41         unsigned nsn = get_n_snap_nodes();
42         for(unsigned i=0; i<nsn; ++i)
43         {
44                 Snap sn = get_snap_node(i);
45                 Snap ssn = sn;
46                 if(other.snap(ssn, limit, what))
47                 {
48                         set_rotation(rotation+ssn.rotation-sn.rotation-M_PI);
49                         sn = get_snap_node(i);
50                         position.x += ssn.position.x-sn.position.x;
51                         position.y += ssn.position.y-sn.position.y;
52                         position.z += ssn.position.z-sn.position.z;
53                         return true;
54                 }
55         }
56
57         return false;
58 }
59
60 } // namespace R2C2