gusimplewhiteboard
wb_vision_detection_features.c
Go to the documentation of this file.
1/*
2 * file wb_vision_detection_features.c
3 *
4 * This file was generated by classgenerator from vision_detection_features.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_features_description(const struct wb_vision_detection_features* 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#ifdef __APPLE__
134 len += snprintf(descString + len, bufferSize - len, "frameNumber=%llu", self->frameNumber);
135#else
136 len += snprintf(descString + len, bufferSize - len, "frameNumber=%lu", self->frameNumber);
137#endif
138 if (len >= bufferSize) {
139 return descString;
140 }
141 len = gu_strlcat(descString, ", ", bufferSize);
142 if (len >= bufferSize) {
143 return descString;
144 }
145 len = gu_strlcat(descString, "features={", bufferSize);
146 for (int features_index = 0; features_index < VISION_DETECTION_FEATURES_FEATURES_ARRAY_SIZE; features_index++) {
147 if (len >= bufferSize) {
148 return descString;
149 }
150 if (features_index > 0) {
151 len = gu_strlcat(descString, ", ", bufferSize);
152 }
153 len = gu_strlcat(descString, "{", bufferSize);
154 if (len >= bufferSize) {
155 return descString;
156 }
158 char* features_1_p = features_1_buffer;
159 const char* features_1_description = wb_vision_detection_feature_array_description(&self->features[features_index], features_1_p, VISION_DETECTION_FEATURE_ARRAY_DESC_BUFFER_SIZE);
160 len = gu_strlcat(descString, features_1_p, bufferSize);
161 if (len >= bufferSize) {
162 return descString;
163 }
164 len = gu_strlcat(descString, "}", bufferSize);
165 }
166 if (len >= bufferSize) {
167 return descString;
168 }
169 len = gu_strlcat(descString, "}", bufferSize);
170 return descString;
171#pragma clang diagnostic pop
172}
173
177const char* wb_vision_detection_features_to_string(const struct wb_vision_detection_features* self, char* toString, size_t bufferSize)
178{
179#pragma clang diagnostic push
180#pragma clang diagnostic ignored "-Wunused-variable"
181 size_t len = 0;
182 if (len >= bufferSize) {
183 return toString;
184 }
185#ifdef __APPLE__
186 len += snprintf(toString + len, bufferSize - len, "%llu", self->frameNumber);
187#else
188 len += snprintf(toString + len, bufferSize - len, "%lu", self->frameNumber);
189#endif
190 if (len >= bufferSize) {
191 return toString;
192 }
193 len = gu_strlcat(toString, ", ", bufferSize);
194 if (len >= bufferSize) {
195 return toString;
196 }
197 len = gu_strlcat(toString, "{", bufferSize);
198 for (int features_index = 0; features_index < VISION_DETECTION_FEATURES_FEATURES_ARRAY_SIZE; features_index++) {
199 if (len >= bufferSize) {
200 return toString;
201 }
202 if (features_index > 0) {
203 len = gu_strlcat(toString, ", ", bufferSize);
204 }
205 len = gu_strlcat(toString, "{", bufferSize);
206 if (len >= bufferSize) {
207 return toString;
208 }
210 char* features_1_p = features_1_buffer;
211 const char* features_1_to_string = wb_vision_detection_feature_array_to_string(&self->features[features_index], features_1_p, VISION_DETECTION_FEATURE_ARRAY_TO_STRING_BUFFER_SIZE);
212 len = gu_strlcat(toString, features_1_p, bufferSize);
213 if (len >= bufferSize) {
214 return toString;
215 }
216 len = gu_strlcat(toString, "}", bufferSize);
217 }
218 if (len >= bufferSize) {
219 return toString;
220 }
221 len = gu_strlcat(toString, "}", bufferSize);
222 return toString;
223#pragma clang diagnostic pop
224}
225
230{
231 size_t temp_length = strlen(str);
232 int length = (temp_length <= INT_MAX) ? ((int)((ssize_t)temp_length)) : -1;
233 if (length < 1 || length > VISION_DETECTION_FEATURES_DESC_BUFFER_SIZE) {
234 return self;
235 }
236 char var_str_buffer[VISION_DETECTION_FEATURES_DESC_BUFFER_SIZE + 1];
237 char* var_str = &var_str_buffer[0];
238 char key_buffer[12];
239 char* key = &key_buffer[0];
240 int bracecount = 0;
241 int lastBrace = -1;
242 int startVar = 0;
243 int index = 0;
244 int startKey = 0;
245 int endKey = -1;
246 int varIndex = 0;
247 if (index == 0 && str[0] == '{') {
248 index = 1;
249 }
250 startVar = index;
251 startKey = startVar;
252 do {
253 for (int i = index; i < length; i++) {
254 index = i + 1;
255 if (bracecount == 0 && str[i] == '=') {
256 endKey = i - 1;
257 startVar = index;
258 continue;
259 }
260 if (bracecount == 0 && isspace(str[i])) {
261 startVar = index;
262 if (endKey == -1) {
263 startKey = index;
264 }
265 continue;
266 }
267 if (bracecount == 0 && str[i] == ',') {
268 index = i - 1;
269 break;
270 }
271 if (str[i] == '{') {
272 bracecount++;
273 if (bracecount == 1) {
274 lastBrace = i;
275 }
276 continue;
277 }
278 if (str[i] == '}') {
279 bracecount--;
280 if (bracecount < 0) {
281 index = i - 1;
282 break;
283 }
284 }
285 if (i == length - 1) {
286 index = i;
287 }
288 }
289 if (endKey >= startKey && endKey - startKey < length) {
290 strncpy(key, str + startKey, ((size_t)(endKey - startKey) + 1));
291 key[(endKey - startKey) + 1] = 0;
292 } else {
293 key[0] = 0;
294 }
295 strncpy(var_str, str + startVar, ((size_t)(index - startVar) + 1));
296 var_str[(index - startVar) + 1] = 0;
297 bracecount = 0;
298 index += 2;
299 startVar = index;
300 startKey = startVar;
301 endKey = -1;
302 if (strlen(key) > 0) {
303 if (0 == strcmp("frameNumber", key)) {
304 varIndex = 0;
305 } else if (0 == strcmp("features", key)) {
306 varIndex = 1;
307 } else {
308 varIndex = -1;
309 }
310 }
311 switch (varIndex) {
312 case -1: { break; }
313 case 0:
314 {
315#ifdef __APPLE__
316 self->frameNumber = ((uint64_t)atoll(var_str));
317#else
318 self->frameNumber = ((uint64_t)atol(var_str));
319#endif
320 break;
321 }
322 case 1:
323 {
324 int restartIndex = index;
325 index = lastBrace + 1;
326 startVar = index;
327 startKey = startVar;
328 endKey = -1;
329 bracecount = 0;
330 for (int features_0_index = 0; features_0_index < VISION_DETECTION_FEATURES_FEATURES_ARRAY_SIZE; features_0_index++) {
331 for (int i = index; i < length; i++) {
332 index = i + 1;
333 if (bracecount == 0 && str[i] == '=') {
334 endKey = i - 1;
335 startVar = index;
336 continue;
337 }
338 if (bracecount == 0 && isspace(str[i])) {
339 startVar = index;
340 if (endKey == -1) {
341 startKey = index;
342 }
343 continue;
344 }
345 if (bracecount == 0 && str[i] == ',') {
346 index = i - 1;
347 break;
348 }
349 if (str[i] == '{') {
350 bracecount++;
351 continue;
352 }
353 if (str[i] == '}') {
354 bracecount--;
355 if (bracecount < 0) {
356 index = i - 1;
357 break;
358 }
359 }
360 if (i == length - 1) {
361 index = i;
362 }
363 }
364 if (endKey >= startKey && endKey - startKey < length) {
365 strncpy(key, str + startKey, ((size_t)(endKey - startKey) + 1));
366 key[(endKey - startKey) + 1] = 0;
367 } else {
368 key[0] = 0;
369 }
370 strncpy(var_str, str + startVar, ((size_t)(index - startVar) + 1));
371 var_str[(index - startVar) + 1] = 0;
372 bracecount = 0;
373 index += 2;
374 startVar = index;
375 startKey = startVar;
376 endKey = -1;
377 struct wb_vision_detection_feature_array features_0 = {};
379 self->features[features_0_index] = features_0;
380 }
381 index = restartIndex;
382 break;
383 }
384 }
385 if (varIndex >= 0) {
386 varIndex++;
387 }
388 } while(index < length);
389 return self;
390}
391
392/*#ifdef WHITEBOARD_SERIALISATION*/
393
398{
399 uint16_t bit_offset = 0;
400 uint64_t frameNumber_nbo = htonll(self->frameNumber);
401 do {
402 int8_t b;
403 for (b = (64 - 1); b >= 0; b--) {
404 do {
405 uint16_t byte = bit_offset / 8;
406 uint16_t bit = 7 - (bit_offset % 8);
407 unsigned long newbit = !!((frameNumber_nbo >> b) & 1U);
408 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
409 bit_offset = bit_offset + 1;
410 } while(false);
411 }
412 } while(false);
413
414 //Class generator does not support array network compression.
415 //Copying into the buffer, uncompressed
416 do { //limit declaration scope
417 uint32_t len = 2;
418 uint32_t bytes = len * sizeof(struct wb_vision_detection_feature_array);
419 const char *buf = (const char *)&self->features[0];
420 uint32_t c;
421 int8_t b;
422 for (c = 0; c < bytes; c++) {
423 for (b = 7; b >= 0; b--) {
424 do {
425 uint16_t byte = bit_offset / 8;
426 uint16_t bit = 7 - (bit_offset % 8);
427 unsigned long newbit = !!((buf[c] >> b) & 1U);
428 dst[byte] ^= (-newbit ^ dst[byte]) & (1UL << bit);
429 bit_offset = bit_offset + 1;
430 } while(false);
431 }
432 }
433 } while(false);
434 //avoid unused variable warnings when you try to use an empty gen file or a gen file with no supported serialisation types.
435 (void)self;
436 (void)dst;
437 return bit_offset;
438}
439
444{
445 uint16_t bit_offset = 0;
446 do {
447 int8_t b;
448 for (b = (64 - 1); b >= 0; b--) {
449 do {
450 uint16_t byte = bit_offset / 8;
451 uint16_t bit = 7 - (bit_offset % 8);
452 char dataByte = src[byte];
453 unsigned char bitValue = (dataByte >> bit) & 1U;
454 dst->frameNumber ^= (-bitValue ^ dst->frameNumber) & (1UL << b);
455 bit_offset = bit_offset + 1;
456 } while(false);
457 }
458 } while(false);
459 dst->frameNumber = ntohll(dst->frameNumber);
460
461 //Class generator does not support array network compression.
462 //Copying into the buffer, uncompressed
463 do { //limit declaration scope
464 uint32_t len = 2;
465 uint32_t bytes = len * sizeof(struct wb_vision_detection_feature_array);
466 char *buf = (char *)malloc(bytes);
467 uint32_t c;
468 int8_t b;
469 for (c = 0; c < bytes; c++) {
470 for (b = 7; b >= 0; b--) {
471 do {
472 uint16_t byte = bit_offset / 8;
473 uint16_t bit = 7 - (bit_offset % 8);
474 char dataByte = src[byte];
475 unsigned char bitValue = (dataByte >> bit) & 1U;
476 buf[c] ^= (-bitValue ^ buf[c]) & (1UL << b);
477 bit_offset = bit_offset + 1;
478 } while(false);
479 }
480 }
481 memcpy(&dst->features[0], &buf[0], bytes);
482 free(buf);
483 } while(false);
484 //avoid unused variable warnings when you try to use an empty gen file or a gen file with no supported serialisation types.
485 (void)src;
486 (void)dst;
487 return bit_offset;
488}
489
490/*#endif // WHITEBOARD_SERIALISATION*/
Results for the Feature Detector (guvision).
Results for the Feature Detector (guvision).
struct wb_vision_detection_feature_array features[2]
Features reported for each camera.
uint64_t frameNumber
The frame number that these lines were seen in.
const char * wb_vision_detection_feature_array_description(const struct wb_vision_detection_feature_array *self, char *descString, size_t bufferSize)
Convert to a description string.
const char * wb_vision_detection_feature_array_to_string(const struct wb_vision_detection_feature_array *self, char *toString, size_t bufferSize)
Convert to a string.
struct wb_vision_detection_feature_array * wb_vision_detection_feature_array_from_string(struct wb_vision_detection_feature_array *self, const char *str)
Convert from a string.
#define VISION_DETECTION_FEATURE_ARRAY_TO_STRING_BUFFER_SIZE
#define VISION_DETECTION_FEATURE_ARRAY_DESC_BUFFER_SIZE
struct wb_vision_detection_features * wb_vision_detection_features_from_string(struct wb_vision_detection_features *self, const char *str)
Convert from a string.
#define ntohll(x)
size_t wb_vision_detection_features_from_network_serialised(const char *src, struct wb_vision_detection_features *dst)
Convert from a compressed, serialised, network byte order byte stream.
#define htonll(x)
const char * wb_vision_detection_features_description(const struct wb_vision_detection_features *self, char *descString, size_t bufferSize)
Convert to a description string.
const char * wb_vision_detection_features_to_string(const struct wb_vision_detection_features *self, char *toString, size_t bufferSize)
Convert to a string.
size_t wb_vision_detection_features_to_network_serialised(const struct wb_vision_detection_features *self, char *dst)
Convert to a compressed, serialised, network byte order byte stream.
#define VISION_DETECTION_FEATURES_DESC_BUFFER_SIZE
#define VISION_DETECTION_FEATURES_FEATURES_ARRAY_SIZE
WHITEBOARD_POSTER_STRING_CONVERSION.