gusimplewhiteboard
wb_vision_detection_horizon.c
Go to the documentation of this file.
1/*
2 * file wb_vision_detection_horizon.c
3 *
4 * This file was generated by classgenerator from vision_detection_horizon.gen.
5 * DO NOT CHANGE MANUALLY!
6 *
7 * Copyright © 2021 Carl Lusty. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer in the documentation and/or other materials
19 * provided with the distribution.
20 *
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgement:
23 *
24 * This product includes software developed by Carl Lusty.
25 *
26 * 4. Neither the name of the author nor the names of contributors
27 * may be used to endorse or promote products derived from this
28 * software without specific prior written permission.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
31 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
32 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
33 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
34 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
35 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
36 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
37 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
38 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
39 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
40 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41 *
42 * -----------------------------------------------------------------------
43 * This program is free software; you can redistribute it and/or
44 * modify it under the above terms or under the terms of the GNU
45 * General Public License as published by the Free Software Foundation;
46 * either version 2 of the License, or (at your option) any later version.
47 *
48 * This program is distributed in the hope that it will be useful,
49 * but WITHOUT ANY WARRANTY; without even the implied warranty of
50 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
51 * GNU General Public License for more details.
52 *
53 * You should have received a copy of the GNU General Public License
54 * along with this program; if not, see http://www.gnu.org/licenses/
55 * or write to the Free Software Foundation, Inc., 51 Franklin Street,
56 * Fifth Floor, Boston, MA 02110-1301, USA.
57 *
58 */
59
60#ifndef WHITEBOARD_POSTER_STRING_CONVERSION
61#define WHITEBOARD_POSTER_STRING_CONVERSION
62#endif // WHITEBOARD_POSTER_STRING_CONVERSION
63
65#include <stdio.h>
66#include <string.h>
67#include <stdlib.h>
68#include <ctype.h>
69#include <limits.h>
70
71/* Network byte order functions */
72#pragma clang diagnostic push
73#pragma clang diagnostic ignored "-Wunused-macros"
74#if defined(__linux)
75# include <endian.h>
76# include <byteswap.h>
77#elif defined(__APPLE__)
78# include <machine/endian.h> //Needed for __BYTE_ORDER
79# include <architecture/byte_order.h> //Needed for byte swap functions
80# define bswap_16(x) NXSwapShort(x)
81# define bswap_32(x) NXSwapInt(x)
82# define bswap_64(x) NXSwapLongLong(x)
83#elif defined(ESP8266)
84# define bswap_16(x) __builtin_bswap16(x)
85# define bswap_32(x) __builtin_bswap32(x)
86# define bswap_64(x) __builtin_bswap64(x)
87#else
88 //Manually define swap macros?
89#endif
90
91#if (!defined(__BYTE_ORDER) && !defined(__LITTLE_ENDIAN)) || (defined(__BYTE_ORDER) && defined(__LITTLE_ENDIAN) && __BYTE_ORDER == __LITTLE_ENDIAN)
92# if !defined(htonll) && !defined(ntohll)
93# define htonll(x) bswap_64(x)
94# define ntohll(x) bswap_64(x)
95# endif
96# if !defined(htonl) && !defined(ntohl)
97# define htonl(x) bswap_32(x)
98# define ntohl(x) bswap_32(x)
99# endif
100# if !defined(htons) && !defined(ntohs)
101# define htons(x) bswap_16(x)
102# define ntohs(x) bswap_16(x)
103# endif
104#else
105# if !defined(htonll) && !defined(ntohll)
106# define htonll(x) (x)
107# define ntohll(x) (x)
108# endif
109# if !defined(htonl) && !defined(ntohl)
110# define htonl(x) (x)
111# define ntohl(x) (x)
112# endif
113# if !defined(htons) && !defined(ntohs)
114# define htons(x) (x)
115# define ntohs(x) (x)
116# endif
117#endif
118#pragma clang diagnostic pop
119
120
121
125const char* wb_vision_detection_horizon_description(const struct wb_vision_detection_horizon* self, char* descString, size_t bufferSize)
126{
127#pragma clang diagnostic push
128#pragma clang diagnostic ignored "-Wunused-variable"
129 size_t len = 0;
130 if (len >= bufferSize) {
131 return descString;
132 }
133 switch (self->horizonType) {
134 case CornerHorizon:
135 {
136 len += snprintf(descString + len, bufferSize - len, "horizonType=CornerHorizon");
137 break;
138 }
139 case HorizonFailed:
140 {
141 len += snprintf(descString + len, bufferSize - len, "horizonType=HorizonFailed");
142 break;
143 }
144 case OnlyField:
145 {
146 len += snprintf(descString + len, bufferSize - len, "horizonType=OnlyField");
147 break;
148 }
149 case SingleHorizon:
150 {
151 len += snprintf(descString + len, bufferSize - len, "horizonType=SingleHorizon");
152 break;
153 }
154 }
155 if (len >= bufferSize) {
156 return descString;
157 }
158 len = gu_strlcat(descString, ", ", bufferSize);
159 if (len >= bufferSize) {
160 return descString;
161 }
162 len = gu_strlcat(descString, "leftCoordinate={", bufferSize);
163 if (len >= bufferSize) {
164 return descString;
165 }
166 char leftCoordinate_buffer[PIXEL_COORDINATE_DESC_BUFFER_SIZE];
167 char* leftCoordinate_p = leftCoordinate_buffer;
168 const char* leftCoordinate_description = wb_pixel_coordinate_description(&self->leftCoordinate, leftCoordinate_p, PIXEL_COORDINATE_DESC_BUFFER_SIZE);
169 len = gu_strlcat(descString, leftCoordinate_p, bufferSize);
170 if (len >= bufferSize) {
171 return descString;
172 }
173 len = gu_strlcat(descString, "}", bufferSize);
174 if (len >= bufferSize) {
175 return descString;
176 }
177 len = gu_strlcat(descString, ", ", bufferSize);
178 if (len >= bufferSize) {
179 return descString;
180 }
181 len = gu_strlcat(descString, "centerCoordinate={", bufferSize);
182 if (len >= bufferSize) {
183 return descString;
184 }
185 char centerCoordinate_buffer[PIXEL_COORDINATE_DESC_BUFFER_SIZE];
186 char* centerCoordinate_p = centerCoordinate_buffer;
187 const char* centerCoordinate_description = wb_pixel_coordinate_description(&self->centerCoordinate, centerCoordinate_p, PIXEL_COORDINATE_DESC_BUFFER_SIZE);
188 len = gu_strlcat(descString, centerCoordinate_p, bufferSize);
189 if (len >= bufferSize) {
190 return descString;
191 }
192 len = gu_strlcat(descString, "}", bufferSize);
193 if (len >= bufferSize) {
194 return descString;
195 }
196 len = gu_strlcat(descString, ", ", bufferSize);
197 if (len >= bufferSize) {
198 return descString;
199 }
200 len = gu_strlcat(descString, "rightCoordinate={", bufferSize);
201 if (len >= bufferSize) {
202 return descString;
203 }
204 char rightCoordinate_buffer[PIXEL_COORDINATE_DESC_BUFFER_SIZE];
205 char* rightCoordinate_p = rightCoordinate_buffer;
206 const char* rightCoordinate_description = wb_pixel_coordinate_description(&self->rightCoordinate, rightCoordinate_p, PIXEL_COORDINATE_DESC_BUFFER_SIZE);
207 len = gu_strlcat(descString, rightCoordinate_p, bufferSize);
208 if (len >= bufferSize) {
209 return descString;
210 }
211 len = gu_strlcat(descString, "}", bufferSize);
212 return descString;
213#pragma clang diagnostic pop
214}
215
219const char* wb_vision_detection_horizon_to_string(const struct wb_vision_detection_horizon* self, char* toString, size_t bufferSize)
220{
221#pragma clang diagnostic push
222#pragma clang diagnostic ignored "-Wunused-variable"
223 size_t len = 0;
224 if (len >= bufferSize) {
225 return toString;
226 }
227 switch (self->horizonType) {
228 case CornerHorizon:
229 {
230 len += snprintf(toString + len, bufferSize - len, "CornerHorizon");
231 break;
232 }
233 case HorizonFailed:
234 {
235 len += snprintf(toString + len, bufferSize - len, "HorizonFailed");
236 break;
237 }
238 case OnlyField:
239 {
240 len += snprintf(toString + len, bufferSize - len, "OnlyField");
241 break;
242 }
243 case SingleHorizon:
244 {
245 len += snprintf(toString + len, bufferSize - len, "SingleHorizon");
246 break;
247 }
248 }
249 if (len >= bufferSize) {
250 return toString;
251 }
252 len = gu_strlcat(toString, ", ", bufferSize);
253 if (len >= bufferSize) {
254 return toString;
255 }
256 len = gu_strlcat(toString, "{", bufferSize);
257 if (len >= bufferSize) {
258 return toString;
259 }
260 char leftCoordinate_buffer[PIXEL_COORDINATE_TO_STRING_BUFFER_SIZE];
261 char* leftCoordinate_p = leftCoordinate_buffer;
262 const char* leftCoordinate_to_string = wb_pixel_coordinate_to_string(&self->leftCoordinate, leftCoordinate_p, PIXEL_COORDINATE_TO_STRING_BUFFER_SIZE);
263 len = gu_strlcat(toString, leftCoordinate_p, bufferSize);
264 if (len >= bufferSize) {
265 return toString;
266 }
267 len = gu_strlcat(toString, "}", bufferSize);
268 if (len >= bufferSize) {
269 return toString;
270 }
271 len = gu_strlcat(toString, ", ", bufferSize);
272 if (len >= bufferSize) {
273 return toString;
274 }
275 len = gu_strlcat(toString, "{", bufferSize);
276 if (len >= bufferSize) {
277 return toString;
278 }
279 char centerCoordinate_buffer[PIXEL_COORDINATE_TO_STRING_BUFFER_SIZE];
280 char* centerCoordinate_p = centerCoordinate_buffer;
281 const char* centerCoordinate_to_string = wb_pixel_coordinate_to_string(&self->centerCoordinate, centerCoordinate_p, PIXEL_COORDINATE_TO_STRING_BUFFER_SIZE);
282 len = gu_strlcat(toString, centerCoordinate_p, bufferSize);
283 if (len >= bufferSize) {
284 return toString;
285 }
286 len = gu_strlcat(toString, "}", bufferSize);
287 if (len >= bufferSize) {
288 return toString;
289 }
290 len = gu_strlcat(toString, ", ", bufferSize);
291 if (len >= bufferSize) {
292 return toString;
293 }
294 len = gu_strlcat(toString, "{", bufferSize);
295 if (len >= bufferSize) {
296 return toString;
297 }
298 char rightCoordinate_buffer[PIXEL_COORDINATE_TO_STRING_BUFFER_SIZE];
299 char* rightCoordinate_p = rightCoordinate_buffer;
300 const char* rightCoordinate_to_string = wb_pixel_coordinate_to_string(&self->rightCoordinate, rightCoordinate_p, PIXEL_COORDINATE_TO_STRING_BUFFER_SIZE);
301 len = gu_strlcat(toString, rightCoordinate_p, bufferSize);
302 if (len >= bufferSize) {
303 return toString;
304 }
305 len = gu_strlcat(toString, "}", bufferSize);
306 return toString;
307#pragma clang diagnostic pop
308}
309
314{
315 size_t temp_length = strlen(str);
316 int length = (temp_length <= INT_MAX) ? ((int)((ssize_t)temp_length)) : -1;
317 if (length < 1 || length > VISION_DETECTION_HORIZON_DESC_BUFFER_SIZE) {
318 return self;
319 }
320 char var_str_buffer[VISION_DETECTION_HORIZON_DESC_BUFFER_SIZE + 1];
321 char* var_str = &var_str_buffer[0];
322 char key_buffer[17];
323 char* key = &key_buffer[0];
324 int bracecount = 0;
325 int startVar = 0;
326 int index = 0;
327 int startKey = 0;
328 int endKey = -1;
329 int varIndex = 0;
330 if (index == 0 && str[0] == '{') {
331 index = 1;
332 }
333 startVar = index;
334 startKey = startVar;
335 do {
336 for (int i = index; i < length; i++) {
337 index = i + 1;
338 if (bracecount == 0 && str[i] == '=') {
339 endKey = i - 1;
340 startVar = index;
341 continue;
342 }
343 if (bracecount == 0 && isspace(str[i])) {
344 startVar = index;
345 if (endKey == -1) {
346 startKey = index;
347 }
348 continue;
349 }
350 if (bracecount == 0 && str[i] == ',') {
351 index = i - 1;
352 break;
353 }
354 if (str[i] == '{') {
355 bracecount++;
356 continue;
357 }
358 if (str[i] == '}') {
359 bracecount--;
360 if (bracecount < 0) {
361 index = i - 1;
362 break;
363 }
364 }
365 if (i == length - 1) {
366 index = i;
367 }
368 }
369 if (endKey >= startKey && endKey - startKey < length) {
370 strncpy(key, str + startKey, ((size_t)(endKey - startKey) + 1));
371 key[(endKey - startKey) + 1] = 0;
372 } else {
373 key[0] = 0;
374 }
375 strncpy(var_str, str + startVar, ((size_t)(index - startVar) + 1));
376 var_str[(index - startVar) + 1] = 0;
377 bracecount = 0;
378 index += 2;
379 startVar = index;
380 startKey = startVar;
381 endKey = -1;
382 if (strlen(key) > 0) {
383 if (0 == strcmp("horizonType", key)) {
384 varIndex = 0;
385 } else if (0 == strcmp("leftCoordinate", key)) {
386 varIndex = 1;
387 } else if (0 == strcmp("centerCoordinate", key)) {
388 varIndex = 2;
389 } else if (0 == strcmp("rightCoordinate", key)) {
390 varIndex = 3;
391 } else {
392 varIndex = -1;
393 }
394 }
395 switch (varIndex) {
396 case -1: { break; }
397 case 0:
398 {
399 if (strcmp("CornerHorizon", var_str) == 0) {
400#pragma clang diagnostic push
401#pragma clang diagnostic ignored "-Wbad-function-cast"
402 self->horizonType = CornerHorizon;
403#pragma clang diagnostic pop
404 } else if (strcmp("HorizonFailed", var_str) == 0) {
405#pragma clang diagnostic push
406#pragma clang diagnostic ignored "-Wbad-function-cast"
407 self->horizonType = HorizonFailed;
408#pragma clang diagnostic pop
409 } else if (strcmp("OnlyField", var_str) == 0) {
410#pragma clang diagnostic push
411#pragma clang diagnostic ignored "-Wbad-function-cast"
412 self->horizonType = OnlyField;
413#pragma clang diagnostic pop
414 } else if (strcmp("SingleHorizon", var_str) == 0) {
415#pragma clang diagnostic push
416#pragma clang diagnostic ignored "-Wbad-function-cast"
417 self->horizonType = SingleHorizon;
418#pragma clang diagnostic pop
419 } else {
420#pragma clang diagnostic push
421#pragma clang diagnostic ignored "-Wbad-function-cast"
422 self->horizonType = ((enum HorizonOptions)atoi(var_str));
423#pragma clang diagnostic pop
424 }
425 break;
426 }
427 case 1:
428 {
429 wb_pixel_coordinate_from_string(&self->leftCoordinate, var_str);
430 break;
431 }
432 case 2:
433 {
434 wb_pixel_coordinate_from_string(&self->centerCoordinate, var_str);
435 break;
436 }
437 case 3:
438 {
439 wb_pixel_coordinate_from_string(&self->rightCoordinate, var_str);
440 break;
441 }
442 }
443 if (varIndex >= 0) {
444 varIndex++;
445 }
446 } while(index < length);
447 return self;
448}
449
450/*#ifdef WHITEBOARD_SERIALISATION*/
451
456{
457 uint16_t bit_offset = 0;
458 enum HorizonOptions horizonType_nbo = htonl(self->horizonType);
459 do {
460 int8_t b;
461 for (b = (32 - 1); b >= 0; b--) {
462 do {
463 uint16_t byte = bit_offset / 8;
464 uint16_t bit = 7 - (bit_offset % 8);
465 unsigned long newbit = !!((horizonType_nbo >> b) & 1U);
466 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
467 bit_offset = bit_offset + 1;
468 } while(false);
469 }
470 } while(false);
471 //avoid unused variable warnings when you try to use an empty gen file or a gen file with no supported serialisation types.
472 (void)self;
473 (void)dst;
474 return bit_offset;
475}
476
481{
482 uint16_t bit_offset = 0;
483 do {
484 int8_t b;
485 for (b = (32 - 1); b >= 0; b--) {
486 do {
487 uint16_t byte = bit_offset / 8;
488 uint16_t bit = 7 - (bit_offset % 8);
489 char dataByte = src[byte];
490 unsigned char bitValue = (dataByte >> bit) & 1U;
491 dst->horizonType ^= (-bitValue ^ dst->horizonType) & (1UL << b);
492 bit_offset = bit_offset + 1;
493 } while(false);
494 }
495 } while(false);
496 dst->horizonType = ntohl(dst->horizonType);
497 //avoid unused variable warnings when you try to use an empty gen file or a gen file with no supported serialisation types.
498 (void)src;
499 (void)dst;
500 return bit_offset;
501}
502
503/*#endif // WHITEBOARD_SERIALISATION*/
504
505gu_pixel_coordinate wb_vision_detection_horizon_leftCoordinate_px_coord(const struct wb_vision_detection_horizon self, const uint16_t resWidth, const uint16_t resHeight)
506{
507 return wb_px_coord_to_px_coord(self.leftCoordinate, resWidth, resHeight);
508}
509gu_pixel_coordinate wb_vision_detection_horizon_centerCoordinate_px_coord(const struct wb_vision_detection_horizon self, const uint16_t resWidth, const uint16_t resHeight)
510{
511 return wb_px_coord_to_px_coord(self.centerCoordinate, resWidth, resHeight);
512}
513gu_pixel_coordinate wb_vision_detection_horizon_rightCoordinate_px_coord(const struct wb_vision_detection_horizon self, const uint16_t resWidth, const uint16_t resHeight)
514{
515 return wb_px_coord_to_px_coord(self.rightCoordinate, resWidth, resHeight);
516}
WHITEBOARD_POSTER_STRING_CONVERSION.
enum HorizonOptions horizonType
Type of Horizon.
struct wb_pixel_coordinate leftCoordinate
Left horizon point pixel coordinate.
struct wb_pixel_coordinate rightCoordinate
Right horizon point pixel coordinate.
struct wb_pixel_coordinate centerCoordinate
Center horizon point pixel coordinate.
const char * wb_pixel_coordinate_description(const struct wb_pixel_coordinate *self, char *descString, size_t bufferSize)
Convert to a description string.
struct wb_pixel_coordinate * wb_pixel_coordinate_from_string(struct wb_pixel_coordinate *self, const char *str)
Convert from a string.
const char * wb_pixel_coordinate_to_string(const struct wb_pixel_coordinate *self, char *toString, size_t bufferSize)
Convert to a string.
gu_pixel_coordinate wb_px_coord_to_px_coord(const struct wb_pixel_coordinate coord, const uint16_t resWidth, const uint16_t resHeight)
#define PIXEL_COORDINATE_DESC_BUFFER_SIZE
#define PIXEL_COORDINATE_TO_STRING_BUFFER_SIZE
size_t wb_vision_detection_horizon_from_network_serialised(const char *src, struct wb_vision_detection_horizon *dst)
Convert from a compressed, serialised, network byte order byte stream.
gu_pixel_coordinate wb_vision_detection_horizon_leftCoordinate_px_coord(const struct wb_vision_detection_horizon self, const uint16_t resWidth, const uint16_t resHeight)
WHITEBOARD_POSTER_STRING_CONVERSION.
const char * wb_vision_detection_horizon_to_string(const struct wb_vision_detection_horizon *self, char *toString, size_t bufferSize)
Convert to a string.
struct wb_vision_detection_horizon * wb_vision_detection_horizon_from_string(struct wb_vision_detection_horizon *self, const char *str)
Convert from a string.
gu_pixel_coordinate wb_vision_detection_horizon_rightCoordinate_px_coord(const struct wb_vision_detection_horizon self, const uint16_t resWidth, const uint16_t resHeight)
const char * wb_vision_detection_horizon_description(const struct wb_vision_detection_horizon *self, char *descString, size_t bufferSize)
Convert to a description string.
#define ntohl(x)
gu_pixel_coordinate wb_vision_detection_horizon_centerCoordinate_px_coord(const struct wb_vision_detection_horizon self, const uint16_t resWidth, const uint16_t resHeight)
size_t wb_vision_detection_horizon_to_network_serialised(const struct wb_vision_detection_horizon *self, char *dst)
Convert to a compressed, serialised, network byte order byte stream.
#define htonl(x)
#define VISION_DETECTION_HORIZON_DESC_BUFFER_SIZE