libcarte2d.c
Go to the documentation of this file.
1 // Copyright (c) 2010-2016 The YP-Spur Authors, except where otherwise indicated.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to
5 // deal in the Software without restriction, including without limitation the
6 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 // sell copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 // SOFTWARE.
20 
21 #include <math.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 
26 #include <cartesian2d.h>
27 
29 
30 /* 座標系の生成 */
31 CSptr CS_add(CSptr parent_cs, double x, double y, double theta)
32 {
33  CSptr a_cs, p_cs;
34 
35  /* 新しい座標系を作成 */
36  a_cs = (CSptr)malloc(sizeof(CoordinateSystem));
37  if (!a_cs)
38  return (CSptr)0; /* 作成失敗 */
39 
40  if (parent_cs == 0)
41  { /* 座標系の神 */
42  CSroot_ptr = a_cs;
43  a_cs->parent = 0;
44  a_cs->brother = 0;
45  a_cs->child = 0;
46  a_cs->x = 0;
47  a_cs->y = 0;
48  a_cs->theta = 0;
49  a_cs->level = 0;
50  }
51  else
52  { /* 誰かの子 */
53  p_cs = parent_cs;
54 
55  if (!p_cs->child)
56  { /* 長男 */
57  p_cs->child = a_cs;
58  }
59  else
60  { /* 既に兄がいる */
61  p_cs = p_cs->child;
62  while (p_cs->brother)
63  p_cs = p_cs->brother;
64  p_cs->brother = a_cs;
65  }
66  a_cs->parent = parent_cs;
67  a_cs->brother = 0;
68  a_cs->child = 0;
69  a_cs->x = x;
70  a_cs->y = y;
71  a_cs->theta = theta;
72  a_cs->level = parent_cs->level + 1;
73  }
74 
75  return a_cs;
76 }
77 
78 /* 座標系の削除(子・兄弟もろとも) */
79 /* 実装中途半端 */
80 int CS_delete(CSptr target_cs)
81 {
82  if (target_cs->child)
83  {
84  CS_delete(target_cs->child);
85  }
86 
87  if (target_cs->brother)
88  {
89  CS_delete(target_cs->brother);
90  }
91 
92  if (target_cs)
93  {
94  free((void *)target_cs);
95  }
96  return 1;
97 }
98 
99 /* 座標系の設定 */
100 int CS_set(CSptr target_cs, double x, double y, double theta)
101 {
102  if (!target_cs)
103  return 0;
104 
105  target_cs->x = x;
106  target_cs->y = y;
107  target_cs->theta = theta;
108 
109  return 1;
110 }
111 
112 /* ある座標系上の座標を入力 */
113 int CS_set_on_CS(CSptr target_cs, CSptr on_cs, double x, double y, double theta)
114 {
115  if (!target_cs || !on_cs)
116  return 0;
117  if (target_cs->parent)
118  CS_recursive_trans(target_cs->parent, on_cs, &x, &y, &theta);
119  return 1;
120 }
121 
122 /* 子から見た親の位置を、親から見た自分の位置に変換する */
123 void CS_turn_base(double *x, double *y, double *theta)
124 {
125  double xx, yy;
126 
127  xx = -(*x) * cos(-(*theta)) + (*y) * sin(-(*theta));
128  yy = -(*x) * sin(-(*theta)) - (*y) * cos(-(*theta));
129  *theta = -(*theta);
130  *x = xx;
131  *y = yy;
132 }
133 
134 /*-----------------座標変換まわり-----------------*/
135 /* 目的の座標系へひとっとび? */
136 /*               */
137 void CS_recursive_trans(CSptr target_cs, CSptr now_cs, double *x, double *y, double *theta)
138 {
139  /* 座標系が有効か */
140  if (!target_cs || !now_cs)
141  return;
142  // printf("now %d\n",now_cs->level);
143  /* 同じ座標系か(case A) */
144  if (target_cs == now_cs)
145  return; /* 終了 */
146 
147  /* 下の座標系か */
148  if (target_cs->level == now_cs->level)
149  { /* 同じレベルにいるけど違う座標系 */
150  // printf(".down from %d\n",now_cs->level);
151  inv_trans_cs(now_cs, x, y, theta); /* 座標系をひとつ下る */
152  CS_recursive_trans(target_cs->parent, now_cs->parent, x, y, theta);
153  // printf(".up to %d\n" ,target_cs->level);
154  trans_cs(target_cs, x, y, theta); /* 座標系をひとつ登る */
155  }
156  else if (target_cs->level > now_cs->level)
157  { /* 現在位置の方が下 case C */
158  CS_recursive_trans(target_cs->parent, now_cs, x, y, theta); /* ひとつ下る */
159  // printf("up to %d\n",target_cs->level);
160  trans_cs(target_cs, x, y, theta); /* 座標系をひとつ登る */
161  }
162  else
163  { /* 現在位置の方が上 case D */
164  // printf("down from %d\n" ,now_cs->level);
165  inv_trans_cs(now_cs, x, y, theta); /* 座標系をひとつ下る */
166  CS_recursive_trans(target_cs, now_cs->parent, x, y, theta); /* ひとつ下る */
167  }
168 
169  return;
170 }
171 
172 /* 座標系を一段下る(1段前の座標系での座標に変換する) */
173 void inv_trans_cs(CSptr target_cs, double *x, double *y, double *theta)
174 {
175  double xx, yy;
176  if (target_cs)
177  {
178  xx = *x * cos(target_cs->theta) - *y * sin(target_cs->theta) + target_cs->x;
179  yy = *x * sin(target_cs->theta) + *y * cos(target_cs->theta) + target_cs->y;
180  *x = xx;
181  *y = yy;
182  *theta += target_cs->theta;
183  }
184 }
185 
186 /* 座標系を一段あがる(一段後での座標系での座標に変換する) */
187 void trans_cs(CSptr target_cs, double *x, double *y, double *theta)
188 {
189  double xx, yy;
190  if (target_cs)
191  {
192  xx = (*x - target_cs->x) * cos(-target_cs->theta) - (*y - target_cs->y) * sin(-target_cs->theta);
193  yy = (*x - target_cs->x) * sin(-target_cs->theta) + (*y - target_cs->y) * cos(-target_cs->theta);
194  *x = xx;
195  *y = yy;
196  *theta -= target_cs->theta;
197  }
198 }
199 
200 /* 上にあがるだけ */
201 void trace_trans_cs(CSptr target_cs, double *x, double *y, double *theta)
202 {
203  if (target_cs == CSroot_ptr)
204  return;
205  trace_trans_cs(target_cs->parent, x, y, theta);
206  trans_cs(target_cs, x, y, theta);
207 
208  return;
209 }
double theta
Definition: cartesian2d.h:38
void inv_trans_cs(CSptr target_cs, double *x, double *y, double *theta)
Definition: libcarte2d.c:173
void trace_trans_cs(CSptr target_cs, double *x, double *y, double *theta)
Definition: libcarte2d.c:201
int CS_set(CSptr target_cs, double x, double y, double theta)
Definition: libcarte2d.c:100
CSptr parent
Definition: cartesian2d.h:39
int level
Definition: cartesian2d.h:42
CSptr CS_add(CSptr parent_cs, double x, double y, double theta)
Definition: libcarte2d.c:31
double y
Definition: cartesian2d.h:37
struct cs_t * CSptr
Definition: cartesian2d.h:33
int CS_set_on_CS(CSptr target_cs, CSptr on_cs, double x, double y, double theta)
Definition: libcarte2d.c:113
void trans_cs(CSptr target_cs, double *x, double *y, double *theta)
Definition: libcarte2d.c:187
double x
Definition: cartesian2d.h:36
CSptr child
Definition: cartesian2d.h:40
void CS_recursive_trans(CSptr target_cs, CSptr now_cs, double *x, double *y, double *theta)
Definition: libcarte2d.c:137
int CS_delete(CSptr target_cs)
Definition: libcarte2d.c:80
void CS_turn_base(double *x, double *y, double *theta)
Definition: libcarte2d.c:123
CSptr brother
Definition: cartesian2d.h:41
CSptr CSroot_ptr
Definition: libcarte2d.c:28


yp-spur
Author(s):
autogenerated on Sat May 11 2019 02:08:24