Blender V5.3
unit.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <algorithm> /* For `min/max`. */
10#include <cctype>
11#include <cstdio>
12#include <cstdlib>
13#include <cstring>
14
15#include "BLI_math_base_c.hh"
16#include "BLI_string.hh"
17#include "BLI_string_utf8.hh"
19#include "BLI_sys_types.hh"
20
21#include "DNA_scene_types.h"
22
23#include "BKE_unit.hh" /* own include */
24
25#ifdef WIN32
26# include "BLI_winstuff.hh"
27#endif
28
29namespace blender {
30
31/* No BKE or DNA includes! */
32
33/* Keep alignment. */
34/* clang-format off */
35
36
37#define TEMP_STR_SIZE 256
38
39
40#define SEP_CHR '#'
41#define SEP_STR "#"
42
43
44#define EPS 0.001
45
46
47#define UN_SC_KM 1000.0f
48#define UN_SC_HM 100.0f
49#define UN_SC_DAM 10.0f
50#define UN_SC_M 1.0f
51#define UN_SC_DM 0.1f
52#define UN_SC_CM 0.01f
53#define UN_SC_MM 0.001f
54#define UN_SC_UM 0.000001f
55
56
57#define UN_SC_MI 1609.344f
58#define UN_SC_FUR 201.168f
59#define UN_SC_CH 20.1168f
60#define UN_SC_YD 0.9144f
61#define UN_SC_FT 0.3048f
62#define UN_SC_IN 0.0254f
63#define UN_SC_MIL 0.0000254f
64
65
66#define UN_SC_MTON 1000.0f /* Metric ton. */
67#define UN_SC_QL 100.0f
68#define UN_SC_KG 1.0f
69#define UN_SC_HG 0.1f
70#define UN_SC_DAG 0.01f
71#define UN_SC_G 0.001f
72#define UN_SC_MG 0.000001f
73
74
75#define UN_SC_ITON 907.18474f /* Imperial ton. */
76#define UN_SC_CWT 45.359237f
77#define UN_SC_ST 6.35029318f
78#define UN_SC_LB 0.45359237f
79#define UN_SC_OZ 0.028349523125f
80
81
82#define UN_SC_FAH 0.555555555555f
83
84/* clang-format on */
85
86/* Define a single unit.
87 * When changing the format, please check that the PYGETTEXT_KEYWORDS regex
88 * used to extract the unit names for translation still works
89 * in scripts/modules/_bl_i18n_utils/settings.py. */
90struct bUnitDef {
91 const char *name;
93 const char *name_plural;
95 const char *name_short;
100 const char *name_alt;
101
103 const char *name_display;
105 const char *identifier;
106
107 double scalar;
109 double bias;
110 int flag;
111};
112
113enum {
123};
124
125/* Define a single unit system. */
135
136#define UNIT_COLLECTION_LENGTH(def) (ARRAY_SIZE(def) - 1)
137
138/* Clang-format wraps this define badly. */
139/* clang-format off */
140#define NULL_UNIT { \
141 /*name*/ nullptr, \
142 /*name_plural*/ nullptr, \
143 /*name_short*/ nullptr, \
144 /*name_alt*/ nullptr, \
145 /*name_display*/ nullptr, \
146 /*identifier*/ nullptr, \
147 /*scalar*/ 0.0, \
148 /*bias*/ 0.0, \
149 /*flag*/ 0, \
150 }
151/* clang-format on */
152
153/* Dummy */
155 {
156 /*name*/ "",
157 /*name_plural*/ nullptr,
158 /*name_short*/ "",
159 /*name_alt*/ nullptr,
160 /*name_display*/ nullptr,
161 /*identifier*/ nullptr,
162 /*scalar*/ 1.0,
163 /*bias*/ 0.0,
164 /*flag*/ 0,
165 },
166 NULL_UNIT,
167};
168
170 /*units*/ buDummyDef,
171 /*base_unit*/ 0,
172 /*flag*/ 0,
174};
175
176/* Lengths. */
178 {
179 /*name*/ "kilometer",
180 /*name_plural*/ "kilometers",
181 /*name_short*/ "km",
182 /*name_alt*/ nullptr,
183 /*name_display*/ "Kilometers",
184 /*identifier*/ "KILOMETERS",
185 /*scalar*/ UN_SC_KM,
186 /*bias*/ 0.0,
187 /*flag*/ B_UNIT_DEF_NONE,
188 },
189 {
190 /*name*/ "hectometer",
191 /*name_plural*/ "hectometers",
192 /*name_short*/ "hm",
193 /*name_alt*/ nullptr,
194 /*name_display*/ "100 Meters",
195 /*identifier*/ "HECTOMETERS",
196 /*scalar*/ UN_SC_HM,
197 /*bias*/ 0.0,
198 /*flag*/ B_UNIT_DEF_SUPPRESS,
199 },
200 {
201 /*name*/ "dekameter",
202 /*name_plural*/ "dekameters",
203 /*name_short*/ "dam",
204 /*name_alt*/ nullptr,
205 /*name_display*/ "10 Meters",
206 /*identifier*/ "DEKAMETERS",
207 /*scalar*/ UN_SC_DAM,
208 /*bias*/ 0.0,
209 /*flag*/ B_UNIT_DEF_SUPPRESS,
210 },
211 /* Base unit. */
212 {
213 /*name*/ "meter",
214 /*name_plural*/ "meters",
215 /*name_short*/ "m",
216 /*name_alt*/ nullptr,
217 /*name_display*/ "Meters",
218 /*identifier*/ "METERS",
219 /*scalar*/ UN_SC_M,
220 /*bias*/ 0.0,
221 /*flag*/ B_UNIT_DEF_NONE,
222 },
223 {
224 /*name*/ "decimeter",
225 /*name_plural*/ "decimeters",
226 /*name_short*/ "dm",
227 /*name_alt*/ nullptr,
228 /*name_display*/ "10 Centimeters",
229 /*identifier*/ "DECIMETERS",
230 /*scalar*/ UN_SC_DM,
231 /*bias*/ 0.0,
232 /*flag*/ B_UNIT_DEF_SUPPRESS,
233 },
234 {
235 /*name*/ "centimeter",
236 /*name_plural*/ "centimeters",
237 /*name_short*/ "cm",
238 /*name_alt*/ nullptr,
239 /*name_display*/ "Centimeters",
240 /*identifier*/ "CENTIMETERS",
241 /*scalar*/ UN_SC_CM,
242 /*bias*/ 0.0,
243 /*flag*/ B_UNIT_DEF_NONE,
244 },
245 {
246 /*name*/ "millimeter",
247 /*name_plural*/ "millimeters",
248 /*name_short*/ "mm",
249 /*name_alt*/ nullptr,
250 /*name_display*/ "Millimeters",
251 /*identifier*/ "MILLIMETERS",
252 /*scalar*/ UN_SC_MM,
253 /*bias*/ 0.0,
255 },
256 {
257 /*name*/ "micrometer",
258 /*name_plural*/ "micrometers",
259 /*name_short*/ "µm",
260 /*name_alt*/ "um",
261 /*name_display*/ "Micrometers",
262 /*identifier*/ "MICROMETERS",
263 /*scalar*/ UN_SC_UM,
264 /*bias*/ 0.0,
265 /*flag*/ B_UNIT_DEF_NONE,
266 },
267
268/* These get displayed because of float precision problems in the transform header,
269 * could work around, but for now probably people won't use these. */
270#if 0
271 {
272 /*name*/ "nanometer",
273 /*name_plural*/ "nanometers",
274 /*name_short*/ "nm",
275 /*name_alt*/ nullptr,
276 /*name_display*/ "Nanometers",
277 /*identifier*/ "NANOMETERS",
278 /*scalar*/ 0.000000001,
279 /*bias*/ 0.0,
280 /*flag*/ B_UNIT_DEF_NONE,
281 },
282 {
283 /*name*/ "picometer",
284 /*name_plural*/ "picometers",
285 /*name_short*/ "pm",
286 /*name_alt*/ nullptr,
287 /*name_display*/ "Picometers",
288 /*identifier*/ "PICOMETERS",
289 /*scalar*/ 0.000000000001,
290 /*bias*/ 0.0,
291 /*flag*/ B_UNIT_DEF_NONE,
292 },
293#endif
294 NULL_UNIT,
295};
297 /*units*/ buMetricLenDef,
298 /*base_unit*/ 3,
299 /*flag*/ 0,
301};
302
304 {
305 /*name*/ "mile",
306 /*name_plural*/ "miles",
307 /*name_short*/ "mi",
308 /*name_alt*/ nullptr,
309 /*name_display*/ "Miles",
310 /*identifier*/ "MILES",
311 /*scalar*/ UN_SC_MI,
312 /*bias*/ 0.0,
313 /*flag*/ B_UNIT_DEF_NONE,
314 },
315 {
316 /*name*/ "furlong",
317 /*name_plural*/ "furlongs",
318 /*name_short*/ "fur",
319 /*name_alt*/ nullptr,
320 /*name_display*/ "Furlongs",
321 /*identifier*/ "FURLONGS",
322 /*scalar*/ UN_SC_FUR,
323 /*bias*/ 0.0,
324 /*flag*/ B_UNIT_DEF_SUPPRESS,
325 },
326 {
327 /*name*/ "chain",
328 /*name_plural*/ "chains",
329 /*name_short*/ "ch",
330 /*name_alt*/ nullptr,
331 /*name_display*/ "Chains",
332 /*identifier*/ "CHAINS",
333 /*scalar*/ UN_SC_CH,
334 /*bias*/ 0.0,
335 /*flag*/ B_UNIT_DEF_SUPPRESS,
336 },
337 {
338 /*name*/ "yard",
339 /*name_plural*/ "yards",
340 /*name_short*/ "yd",
341 /*name_alt*/ nullptr,
342 /*name_display*/ "Yards",
343 /*identifier*/ "YARDS",
344 /*scalar*/ UN_SC_YD,
345 /*bias*/ 0.0,
346 /*flag*/ B_UNIT_DEF_SUPPRESS,
347 },
348 /* Base unit. */
349 {
350 /*name*/ "foot",
351 /*name_plural*/ "feet",
352 /*name_short*/ "'",
353 /*name_alt*/ "ft",
354 /*name_display*/ "Feet",
355 /*identifier*/ "FEET",
356 /*scalar*/ UN_SC_FT,
357 /*bias*/ 0.0,
359 },
360 {
361 /*name*/ "inch",
362 /*name_plural*/ "inches",
363 /*name_short*/ "\"",
364 /*name_alt*/ "in",
365 /*name_display*/ "Inches",
366 /*identifier*/ "INCHES",
367 /*scalar*/ UN_SC_IN,
368 /*bias*/ 0.0,
370 },
371 /* NOTE: Plural for "thou" has no 's'. */
372 {
373 /*name*/ "thou",
374 /*name_plural*/ "thou",
375 /*name_short*/ "thou",
376 /*name_alt*/ "mil",
377 /*name_display*/ "Thou",
378 /*identifier*/ "THOU",
379 /*scalar*/ UN_SC_MIL,
380 /*bias*/ 0.0,
381 /*flag*/ B_UNIT_DEF_NONE,
382 },
383 NULL_UNIT,
384};
386 /*units*/ buImperialLenDef,
387 /*base_unit*/ 4,
388 /*flag*/ 0,
390};
391
392/* Wavelengths (scene-independent, with nm as the base unit). */
394 {
395 /*name*/ "millimeter",
396 /*name_plural*/ "millimeters",
397 /*name_short*/ "mm",
398 /*name_alt*/ nullptr,
399 /*name_display*/ "Millimeters",
400 /*identifier*/ nullptr,
401 /*scalar*/ 1e6f,
402 /*bias*/ 0.0,
403 /*flag*/ B_UNIT_DEF_NONE,
404 },
405 {
406 /*name*/ "micrometer",
407 /*name_plural*/ "micrometers",
408 /*name_short*/ "µm",
409 /*name_alt*/ "um",
410 /*name_display*/ "Micrometers",
411 /*identifier*/ nullptr,
412 /*scalar*/ 1e3f,
413 /*bias*/ 0.0,
414 /*flag*/ B_UNIT_DEF_NONE,
415 },
416 /* Base unit. */
417 {
418 /*name*/ "nanometer",
419 /*name_plural*/ "nanometers",
420 /*name_short*/ "nm",
421 /*name_alt*/ nullptr,
422 /*name_display*/ "Nanometers",
423 /*identifier*/ nullptr,
424 /*scalar*/ 1.0f,
425 /*bias*/ 0.0,
426 /*flag*/ B_UNIT_DEF_NONE,
427 },
428 {
429 /*name*/ "picometer",
430 /*name_plural*/ "picometers",
431 /*name_short*/ "pm",
432 /*name_alt*/ nullptr,
433 /*name_display*/ "Picometers",
434 /*identifier*/ nullptr,
435 /*scalar*/ 1e-3f,
436 /*bias*/ 0.0,
437 /*flag*/ B_UNIT_DEF_NONE,
438 },
439 NULL_UNIT,
440};
442 /*units*/ buWavelengthLenDef,
443 /*base_unit*/ 2,
444 /*flag*/ 0,
446};
447
448/* Areas. */
450 {
451 /*name*/ "square kilometer",
452 /*name_plural*/ "square kilometers",
453 /*name_short*/ "km" BLI_STR_UTF8_SUPERSCRIPT_2,
454 /*name_alt*/ "km2",
455 /*name_display*/ "Square Kilometers",
456 /*identifier*/ nullptr,
457 /*scalar*/ UN_SC_KM * UN_SC_KM,
458 /*bias*/ 0.0,
459 /*flag*/ B_UNIT_DEF_NONE,
460 },
461 {
462 /*name*/ "square hectometer",
463 /*name_plural*/ "square hectometers",
464 /*name_short*/ "hm" BLI_STR_UTF8_SUPERSCRIPT_2,
465 /*name_alt*/ "hm2",
466 /*name_display*/ "Square Hectometers",
467 /*identifier*/ nullptr,
468 /*scalar*/ UN_SC_HM * UN_SC_HM,
469 /*bias*/ 0.0,
470 /*flag*/ B_UNIT_DEF_SUPPRESS,
471 }, /* Hectare. */
472 {
473 /*name*/ "square dekameter",
474 /*name_plural*/ "square dekameters",
475 /*name_short*/ "dam" BLI_STR_UTF8_SUPERSCRIPT_2,
476 /*name_alt*/ "dam2",
477 /*name_display*/ "Square Dekameters",
478 /*identifier*/ nullptr,
479 /*scalar*/ UN_SC_DAM * UN_SC_DAM,
480 /*bias*/ 0.0,
481 /*flag*/ B_UNIT_DEF_SUPPRESS,
482 },
483 /* Base unit. */
484 {
485 /*name*/ "square meter",
486 /*name_plural*/ "square meters",
487 /*name_short*/ "m" BLI_STR_UTF8_SUPERSCRIPT_2,
488 /*name_alt*/ "m2",
489 /*name_display*/ "Square Meters",
490 /*identifier*/ nullptr,
491 /*scalar*/ UN_SC_M * UN_SC_M,
492 /*bias*/ 0.0,
493 /*flag*/ B_UNIT_DEF_NONE,
494 },
495 {
496 /*name*/ "square decimeter",
497 /*name_plural*/ "square decimetees",
498 /*name_short*/ "dm" BLI_STR_UTF8_SUPERSCRIPT_2,
499 /*name_alt*/ "dm2",
500 /*name_display*/ "Square Decimeters",
501 /*identifier*/ nullptr,
502 /*scalar*/ UN_SC_DM * UN_SC_DM,
503 /*bias*/ 0.0,
504 /*flag*/ B_UNIT_DEF_SUPPRESS,
505 },
506 {
507 /*name*/ "square centimeter",
508 /*name_plural*/ "square centimeters",
509 /*name_short*/ "cm" BLI_STR_UTF8_SUPERSCRIPT_2,
510 /*name_alt*/ "cm2",
511 /*name_display*/ "Square Centimeters",
512 /*identifier*/ nullptr,
513 /*scalar*/ UN_SC_CM * UN_SC_CM,
514 /*bias*/ 0.0,
515 /*flag*/ B_UNIT_DEF_NONE,
516 },
517 {
518 /*name*/ "square millimeter",
519 /*name_plural*/ "square millimeters",
520 /*name_short*/ "mm" BLI_STR_UTF8_SUPERSCRIPT_2,
521 /*name_alt*/ "mm2",
522 /*name_display*/ "Square Millimeters",
523 /*identifier*/ nullptr,
524 /*scalar*/ UN_SC_MM * UN_SC_MM,
525 /*bias*/ 0.0,
527 },
528 {
529 /*name*/ "square micrometer",
530 /*name_plural*/ "square micrometers",
531 /*name_short*/ "µm" BLI_STR_UTF8_SUPERSCRIPT_2,
532 /*name_alt*/ "um2",
533 /*name_display*/ "Square Micrometers",
534 /*identifier*/ nullptr,
535 /*scalar*/ UN_SC_UM * UN_SC_UM,
536 /*bias*/ 0.0,
537 /*flag*/ B_UNIT_DEF_NONE,
538 },
539 NULL_UNIT,
540};
542 /*units*/ buMetricAreaDef,
543 /*base_unit*/ 3,
544 /*flag*/ 0,
546};
547
549 {
550 /*name*/ "square mile",
551 /*name_plural*/ "square miles",
552 /*name_short*/ "sq mi",
553 /*name_alt*/ nullptr,
554 /*name_display*/ "Square Miles",
555 /*identifier*/ nullptr,
556 /*scalar*/ UN_SC_MI * UN_SC_MI,
557 /*bias*/ 0.0,
558 /*flag*/ B_UNIT_DEF_NONE,
559 },
560 {
561 /*name*/ "square furlong",
562 /*name_plural*/ "square furlongs",
563 /*name_short*/ "sq fur",
564 /*name_alt*/ nullptr,
565 /*name_display*/ "Square Furlongs",
566 /*identifier*/ nullptr,
567 /*scalar*/ UN_SC_FUR * UN_SC_FUR,
568 /*bias*/ 0.0,
569 /*flag*/ B_UNIT_DEF_SUPPRESS,
570 },
571 {
572 /*name*/ "square chain",
573 /*name_plural*/ "square chains",
574 /*name_short*/ "sq ch",
575 /*name_alt*/ nullptr,
576 /*name_display*/ "Square Chains",
577 /*identifier*/ nullptr,
578 /*scalar*/ UN_SC_CH * UN_SC_CH,
579 /*bias*/ 0.0,
580 /*flag*/ B_UNIT_DEF_SUPPRESS,
581 },
582 {
583 /*name*/ "square yard",
584 /*name_plural*/ "square yards",
585 /*name_short*/ "sq yd",
586 /*name_alt*/ nullptr,
587 /*name_display*/ "Square Yards",
588 /*identifier*/ nullptr,
589 /*scalar*/ UN_SC_YD * UN_SC_YD,
590 /*bias*/ 0.0,
591 /*flag*/ B_UNIT_DEF_NONE,
592 },
593 /* Base unit. */
594 {
595 /*name*/ "square foot",
596 /*name_plural*/ "square feet",
597 /*name_short*/ "sq ft",
598 /*name_alt*/ nullptr,
599 /*name_display*/ "Square Feet",
600 /*identifier*/ nullptr,
601 /*scalar*/ UN_SC_FT * UN_SC_FT,
602 /*bias*/ 0.0,
603 /*flag*/ B_UNIT_DEF_NONE,
604 },
605 {
606 /*name*/ "square inch",
607 /*name_plural*/ "square inches",
608 /*name_short*/ "sq in",
609 /*name_alt*/ nullptr,
610 /*name_display*/ "Square Inches",
611 /*identifier*/ nullptr,
612 /*scalar*/ UN_SC_IN * UN_SC_IN,
613 /*bias*/ 0.0,
614 /*flag*/ B_UNIT_DEF_NONE,
615 },
616 {
617 /*name*/ "square thou",
618 /*name_plural*/ "square thou",
619 /*name_short*/ "sq mil",
620 /*name_alt*/ nullptr,
621 /*name_display*/ "Square Thou",
622 /*identifier*/ nullptr,
623 /*scalar*/ UN_SC_MIL * UN_SC_MIL,
624 /*bias*/ 0.0,
625 /*flag*/ B_UNIT_DEF_NONE,
626 },
627 NULL_UNIT,
628};
630 /*units*/ buImperialAreaDef,
631 /*base_unit*/ 4,
632 /*flag*/ 0,
634};
635
636/* Volumes. */
638 {
639 /*name*/ "cubic kilometer",
640 /*name_plural*/ "cubic kilometers",
641 /*name_short*/ "km" BLI_STR_UTF8_SUPERSCRIPT_3,
642 /*name_alt*/ "km3",
643 /*name_display*/ "Cubic Kilometers",
644 /*identifier*/ nullptr,
645 /*scalar*/ UN_SC_KM * UN_SC_KM * UN_SC_KM,
646 /*bias*/ 0.0,
647 /*flag*/ B_UNIT_DEF_NONE,
648 },
649 {
650 /*name*/ "cubic hectometer",
651 /*name_plural*/ "cubic hectometers",
652 /*name_short*/ "hm" BLI_STR_UTF8_SUPERSCRIPT_3,
653 /*name_alt*/ "hm3",
654 /*name_display*/ "Cubic Hectometers",
655 /*identifier*/ nullptr,
656 /*scalar*/ UN_SC_HM * UN_SC_HM * UN_SC_HM,
657 /*bias*/ 0.0,
658 /*flag*/ B_UNIT_DEF_SUPPRESS,
659 },
660 {
661 /*name*/ "cubic dekameter",
662 /*name_plural*/ "cubic dekameters",
663 /*name_short*/ "dam" BLI_STR_UTF8_SUPERSCRIPT_3,
664 /*name_alt*/ "dam3",
665 /*name_display*/ "Cubic Dekameters",
666 /*identifier*/ nullptr,
667 /*scalar*/ UN_SC_DAM * UN_SC_DAM * UN_SC_DAM,
668 /*bias*/ 0.0,
669 /*flag*/ B_UNIT_DEF_SUPPRESS,
670 },
671 /* Base unit. */
672 {
673 /*name*/ "cubic meter",
674 /*name_plural*/ "cubic meters",
675 /*name_short*/ "m" BLI_STR_UTF8_SUPERSCRIPT_3,
676 /*name_alt*/ "m3",
677 /*name_display*/ "Cubic Meters",
678 /*identifier*/ nullptr,
679 /*scalar*/ UN_SC_M * UN_SC_M * UN_SC_M,
680 /*bias*/ 0.0,
681 /*flag*/ B_UNIT_DEF_NONE,
682 },
683 {
684 /*name*/ "cubic decimeter",
685 /*name_plural*/ "cubic decimeters",
686 /*name_short*/ "dm" BLI_STR_UTF8_SUPERSCRIPT_3,
687 /*name_alt*/ "dm3",
688 /*name_display*/ "Cubic Decimeters",
689 /*identifier*/ nullptr,
690 /*scalar*/ UN_SC_DM * UN_SC_DM * UN_SC_DM,
691 /*bias*/ 0.0,
692 /*flag*/ B_UNIT_DEF_SUPPRESS,
693 },
694 {
695 /*name*/ "cubic centimeter",
696 /*name_plural*/ "cubic centimeters",
697 /*name_short*/ "cm" BLI_STR_UTF8_SUPERSCRIPT_3,
698 /*name_alt*/ "cm3",
699 /*name_display*/ "Cubic Centimeters",
700 /*identifier*/ nullptr,
701 /*scalar*/ UN_SC_CM * UN_SC_CM * UN_SC_CM,
702 /*bias*/ 0.0,
703 /*flag*/ B_UNIT_DEF_NONE,
704 },
705 {
706 /*name*/ "cubic millimeter",
707 /*name_plural*/ "cubic millimeters",
708 /*name_short*/ "mm" BLI_STR_UTF8_SUPERSCRIPT_3,
709 /*name_alt*/ "mm3",
710 /*name_display*/ "Cubic Millimeters",
711 /*identifier*/ nullptr,
712 /*scalar*/ UN_SC_MM * UN_SC_MM * UN_SC_MM,
713 /*bias*/ 0.0,
715 },
716 {
717 /*name*/ "cubic micrometer",
718 /*name_plural*/ "cubic micrometers",
719 /*name_short*/ "µm" BLI_STR_UTF8_SUPERSCRIPT_3,
720 /*name_alt*/ "um3",
721 /*name_display*/ "Cubic Micrometers",
722 /*identifier*/ nullptr,
723 /*scalar*/ UN_SC_UM * UN_SC_UM * UN_SC_UM,
724 /*bias*/ 0.0,
725 /*flag*/ B_UNIT_DEF_NONE,
726 },
727 NULL_UNIT,
728};
730 /*units*/ buMetricVolDef,
731 /*base_unit*/ 3,
732 /*flag*/ 0,
734};
735
737 {
738 /*name*/ "cubic mile",
739 /*name_plural*/ "cubic miles",
740 /*name_short*/ "cu mi",
741 /*name_alt*/ nullptr,
742 /*name_display*/ "Cubic Miles",
743 /*identifier*/ nullptr,
744 /*scalar*/ UN_SC_MI * UN_SC_MI * UN_SC_MI,
745 /*bias*/ 0.0,
746 /*flag*/ B_UNIT_DEF_NONE,
747 },
748 {
749 /*name*/ "cubic furlong",
750 /*name_plural*/ "cubic furlongs",
751 /*name_short*/ "cu fur",
752 /*name_alt*/ nullptr,
753 /*name_display*/ "Cubic Furlongs",
754 /*identifier*/ nullptr,
755 /*scalar*/ UN_SC_FUR * UN_SC_FUR * UN_SC_FUR,
756 /*bias*/ 0.0,
757 /*flag*/ B_UNIT_DEF_SUPPRESS,
758 },
759 {
760 /*name*/ "cubic chain",
761 /*name_plural*/ "cubic chains",
762 /*name_short*/ "cu ch",
763 /*name_alt*/ nullptr,
764 /*name_display*/ "Cubic Chains",
765 /*identifier*/ nullptr,
766 /*scalar*/ UN_SC_CH * UN_SC_CH * UN_SC_CH,
767 /*bias*/ 0.0,
768 /*flag*/ B_UNIT_DEF_SUPPRESS,
769 },
770 {
771 /*name*/ "cubic yard",
772 /*name_plural*/ "cubic yards",
773 /*name_short*/ "cu yd",
774 /*name_alt*/ nullptr,
775 /*name_display*/ "Cubic Yards",
776 /*identifier*/ nullptr,
777 /*scalar*/ UN_SC_YD * UN_SC_YD * UN_SC_YD,
778 /*bias*/ 0.0,
779 /*flag*/ B_UNIT_DEF_NONE,
780 },
781 /* Base unit. */
782 {
783 /*name*/ "cubic foot",
784 /*name_plural*/ "cubic feet",
785 /*name_short*/ "cu ft",
786 /*name_alt*/ nullptr,
787 /*name_display*/ "Cubic Feet",
788 /*identifier*/ nullptr,
789 /*scalar*/ UN_SC_FT * UN_SC_FT * UN_SC_FT,
790 /*bias*/ 0.0,
791 /*flag*/ B_UNIT_DEF_NONE,
792 },
793 {
794 /*name*/ "cubic inch",
795 /*name_plural*/ "cubic inches",
796 /*name_short*/ "cu in",
797 /*name_alt*/ nullptr,
798 /*name_display*/ "Cubic Inches",
799 /*identifier*/ nullptr,
800 /*scalar*/ UN_SC_IN * UN_SC_IN * UN_SC_IN,
801 /*bias*/ 0.0,
802 /*flag*/ B_UNIT_DEF_NONE,
803 },
804 {
805 /*name*/ "cubic thou",
806 /*name_plural*/ "cubic thou",
807 /*name_short*/ "cu mil",
808 /*name_alt*/ nullptr,
809 /*name_display*/ "Cubic Thou",
810 /*identifier*/ nullptr,
811 /*scalar*/ UN_SC_MIL * UN_SC_MIL * UN_SC_MIL,
812 /*bias*/ 0.0,
813 /*flag*/ B_UNIT_DEF_NONE,
814 },
815 NULL_UNIT,
816};
818 /*units*/ buImperialVolDef,
819 /*base_unit*/ 4,
820 /*flag*/ 0,
822};
823
824/* Mass. */
826 {
827 /*name*/ "tonne",
828 /*name_plural*/ "tonnes",
829 /*name_short*/ "t",
830 /*name_alt*/ "ton",
831 /*name_display*/ "Tonnes",
832 /*identifier*/ "TONNES",
833 /*scalar*/ UN_SC_MTON,
834 /*bias*/ 0.0,
835 /*flag*/ B_UNIT_DEF_NONE,
836 },
837 {
838 /*name*/ "quintal",
839 /*name_plural*/ "quintals",
840 /*name_short*/ "ql",
841 /*name_alt*/ "q",
842 /*name_display*/ "100 Kilograms",
843 /*identifier*/ "QUINTALS",
844 /*scalar*/ UN_SC_QL,
845 /*bias*/ 0.0,
846 /*flag*/ B_UNIT_DEF_SUPPRESS,
847 },
848 /* Base unit. */
849 {
850 /*name*/ "kilogram",
851 /*name_plural*/ "kilograms",
852 /*name_short*/ "kg",
853 /*name_alt*/ nullptr,
854 /*name_display*/ "Kilograms",
855 /*identifier*/ "KILOGRAMS",
856 /*scalar*/ UN_SC_KG,
857 /*bias*/ 0.0,
858 /*flag*/ B_UNIT_DEF_NONE,
859 },
860 {
861 /*name*/ "hectogram",
862 /*name_plural*/ "hectograms",
863 /*name_short*/ "hg",
864 /*name_alt*/ nullptr,
865 /*name_display*/ "Hectograms",
866 /*identifier*/ "HECTOGRAMS",
867 /*scalar*/ UN_SC_HG,
868 /*bias*/ 0.0,
869 /*flag*/ B_UNIT_DEF_SUPPRESS,
870 },
871 {
872 /*name*/ "dekagram",
873 /*name_plural*/ "dekagrams",
874 /*name_short*/ "dag",
875 /*name_alt*/ nullptr,
876 /*name_display*/ "10 Grams",
877 /*identifier*/ "DEKAGRAMS",
878 /*scalar*/ UN_SC_DAG,
879 /*bias*/ 0.0,
880 /*flag*/ B_UNIT_DEF_SUPPRESS,
881 },
882 {
883 /*name*/ "gram",
884 /*name_plural*/ "grams",
885 /*name_short*/ "g",
886 /*name_alt*/ nullptr,
887 /*name_display*/ "Grams",
888 /*identifier*/ "GRAMS",
889 /*scalar*/ UN_SC_G,
890 /*bias*/ 0.0,
891 /*flag*/ B_UNIT_DEF_NONE,
892 },
893 {
894 /*name*/ "milligram",
895 /*name_plural*/ "milligrams",
896 /*name_short*/ "mg",
897 /*name_alt*/ nullptr,
898 /*name_display*/ "Milligrams",
899 /*identifier*/ "MILLIGRAMS",
900 /*scalar*/ UN_SC_MG,
901 /*bias*/ 0.0,
902 /*flag*/ B_UNIT_DEF_NONE,
903 },
904 NULL_UNIT,
905};
907 /*units*/ buMetricMassDef,
908 /*base_unit*/ 2,
909 /*flag*/ 0,
911};
912
914 {
915 /*name*/ "ton",
916 /*name_plural*/ "tons",
917 /*name_short*/ "tn",
918 /*name_alt*/ nullptr,
919 /*name_display*/ "Tons",
920 /*identifier*/ "TONNES",
921 /*scalar*/ UN_SC_ITON,
922 /*bias*/ 0.0,
923 /*flag*/ B_UNIT_DEF_NONE,
924 },
925 {
926 /*name*/ "centum weight",
927 /*name_plural*/ "centum weights",
928 /*name_short*/ "cwt",
929 /*name_alt*/ nullptr,
930 /*name_display*/ "Centum weights",
931 /*identifier*/ "CENTUM_WEIGHTS",
932 /*scalar*/ UN_SC_CWT,
933 /*bias*/ 0.0,
934 /*flag*/ B_UNIT_DEF_NONE,
935 },
936 {
937 /*name*/ "stone",
938 /*name_plural*/ "stones",
939 /*name_short*/ "st",
940 /*name_alt*/ nullptr,
941 /*name_display*/ "Stones",
942 /*identifier*/ "STONES",
943 /*scalar*/ UN_SC_ST,
944 /*bias*/ 0.0,
945 /*flag*/ B_UNIT_DEF_NONE,
946 },
947 /* Base unit. */
948 {
949 /*name*/ "pound",
950 /*name_plural*/ "pounds",
951 /*name_short*/ "lb",
952 /*name_alt*/ nullptr,
953 /*name_display*/ "Pounds",
954 /*identifier*/ "POUNDS",
955 /*scalar*/ UN_SC_LB,
956 /*bias*/ 0.0,
957 /*flag*/ B_UNIT_DEF_NONE,
958 },
959 {
960 /*name*/ "ounce",
961 /*name_plural*/ "ounces",
962 /*name_short*/ "oz",
963 /*name_alt*/ nullptr,
964 /*name_display*/ "Ounces",
965 /*identifier*/ "OUNCES",
966 /*scalar*/ UN_SC_OZ,
967 /*bias*/ 0.0,
968 /*flag*/ B_UNIT_DEF_NONE,
969 },
970 NULL_UNIT,
971};
973 /*units*/ buImperialMassDef,
974 /*base_unit*/ 3,
975 /*flag*/ 0,
977};
978
979/* Even if user scales the system to a point where km^3 is used, velocity and
980 * acceleration aren't scaled: that's why we have so few units for them. */
981
982/* Velocity. */
984 /* Base unit. */
985 {
986 /*name*/ "meter per second",
987 /*name_plural*/ "meters per second",
988 /*name_short*/ "m/s",
989 /*name_alt*/ nullptr,
990 /*name_display*/ "Meters per second",
991 /*identifier*/ nullptr,
992 /*scalar*/ UN_SC_M,
993 /*bias*/ 0.0,
994 /*flag*/ B_UNIT_DEF_NONE,
995 },
996 {
997 /*name*/ "kilometer per hour",
998 /*name_plural*/ "kilometers per hour",
999 /*name_short*/ "km/h",
1000 /*name_alt*/ "kph",
1001 /*name_display*/ "Kilometers per hour",
1002 /*identifier*/ nullptr,
1003 /*scalar*/ UN_SC_KM / 3600.0f,
1004 /*bias*/ 0.0,
1005 /*flag*/ B_UNIT_DEF_SUPPRESS,
1006 },
1007 NULL_UNIT,
1008};
1010 /*units*/ buMetricVelDef,
1011 /*base_unit*/ 0,
1012 /*flag*/ 0,
1014};
1015
1017 /* Base unit. */
1018 {
1019 /*name*/ "foot per second",
1020 /*name_plural*/ "feet per second",
1021 /*name_short*/ "ft/s",
1022 /*name_alt*/ "fps",
1023 /*name_display*/ "Feet per second",
1024 /*identifier*/ nullptr,
1025 /*scalar*/ UN_SC_FT,
1026 /*bias*/ 0.0,
1027 /*flag*/ B_UNIT_DEF_NONE,
1028 },
1029 {
1030 /*name*/ "mile per hour",
1031 /*name_plural*/ "miles per hour",
1032 /*name_short*/ "mph",
1033 /*name_alt*/ nullptr,
1034 /*name_display*/ "Miles per hour",
1035 /*identifier*/ nullptr,
1036 /*scalar*/ UN_SC_MI / 3600.0f,
1037 /*bias*/ 0.0,
1038 /*flag*/ B_UNIT_DEF_SUPPRESS,
1039 },
1040 NULL_UNIT,
1041};
1043 /*units*/ buImperialVelDef,
1044 /*base_unit*/ 0,
1045 /*flag*/ 0,
1047};
1048
1049/* Acceleration. */
1051 /* Base unit. */
1052 {
1053 /*name*/ "meter per second squared",
1054 /*name_plural*/ "meters per second squared",
1055 /*name_short*/ "m/s" BLI_STR_UTF8_SUPERSCRIPT_2,
1056 /*name_alt*/ "m/s2",
1057 /*name_display*/ "Meters per second squared",
1058 /*identifier*/ nullptr,
1059 /*scalar*/ UN_SC_M,
1060 /*bias*/ 0.0,
1061 /*flag*/ B_UNIT_DEF_NONE,
1062 },
1063 NULL_UNIT,
1064};
1066 /*units*/ buMetricAclDef,
1067 /*base_unit*/ 0,
1068 /*flag*/ 0,
1070};
1071
1073 /* Base unit. */
1074 {
1075 /*name*/ "foot per second squared",
1076 /*name_plural*/ "feet per second squared",
1077 /*name_short*/ "ft/s" BLI_STR_UTF8_SUPERSCRIPT_2,
1078 /*name_alt*/ "ft/s2",
1079 /*name_display*/ "Feet per second squared",
1080 /*identifier*/ nullptr,
1081 /*scalar*/ UN_SC_FT,
1082 /*bias*/ 0.0,
1083 /*flag*/ B_UNIT_DEF_NONE,
1084 },
1085 NULL_UNIT,
1086};
1088 /*units*/ buImperialAclDef,
1089 /*base_unit*/ 0,
1090 /*flag*/ 0,
1092};
1093
1094/* Time. */
1096 /* Weeks? - probably not needed for Blender. */
1097 {
1098 /*name*/ "day",
1099 /*name_plural*/ "days",
1100 /*name_short*/ "d",
1101 /*name_alt*/ nullptr,
1102 /*name_display*/ "Days",
1103 /*identifier*/ "DAYS",
1104 /*scalar*/ 86400.0,
1105 /*bias*/ 0.0,
1106 /*flag*/ B_UNIT_DEF_NONE,
1107 },
1108 {
1109 /*name*/ "hour",
1110 /*name_plural*/ "hours",
1111 /*name_short*/ "h",
1112 /*name_alt*/ "hr",
1113 /*name_display*/ "Hours",
1114 /*identifier*/ "HOURS",
1115 /*scalar*/ 3600.0,
1116 /*bias*/ 0.0,
1117 /*flag*/ B_UNIT_DEF_NONE,
1118 },
1119 {
1120 /*name*/ "minute",
1121 /*name_plural*/ "minutes",
1122 /*name_short*/ "min",
1123 /*name_alt*/ "m",
1124 /*name_display*/ "Minutes",
1125 /*identifier*/ "MINUTES",
1126 /*scalar*/ 60.0,
1127 /*bias*/ 0.0,
1128 /*flag*/ B_UNIT_DEF_NONE,
1129 },
1130 /* Base unit. */
1131 {
1132 /*name*/ "second",
1133 /*name_plural*/ "seconds",
1134 /*name_short*/ "s",
1135 /*name_alt*/ "sec",
1136 /*name_display*/ "Seconds",
1137 /*identifier*/ "SECONDS",
1138 /*scalar*/ 1.0,
1139 /*bias*/ 0.0,
1140 /*flag*/ B_UNIT_DEF_NONE,
1141 },
1142 {
1143 /*name*/ "millisecond",
1144 /*name_plural*/ "milliseconds",
1145 /*name_short*/ "ms",
1146 /*name_alt*/ nullptr,
1147 /*name_display*/ "Milliseconds",
1148 /*identifier*/ "MILLISECONDS",
1149 /*scalar*/ 0.001,
1150 /*bias*/ 0.0,
1151 /*flag*/ B_UNIT_DEF_NONE,
1152 },
1153 {
1154 /*name*/ "microsecond",
1155 /*name_plural*/ "microseconds",
1156 /*name_short*/ "µs",
1157 /*name_alt*/ "us",
1158 /*name_display*/ "Microseconds",
1159 /*identifier*/ "MICROSECONDS",
1160 /*scalar*/ 0.000001,
1161 /*bias*/ 0.0,
1162 /*flag*/ B_UNIT_DEF_NONE,
1163 },
1164 NULL_UNIT,
1165};
1167 /*units*/ buNaturalTimeDef,
1168 /*base_unit*/ 3,
1169 /*flag*/ 0,
1171};
1172
1174 {
1175 /*name*/ "degree",
1176 /*name_plural*/ "degrees",
1177 /*name_short*/ BLI_STR_UTF8_DEGREE_SIGN,
1178 /*name_alt*/ "d",
1179 /*name_display*/ "Degrees",
1180 /*identifier*/ "DEGREES",
1181 /*scalar*/ M_PI / 180.0,
1182 /*bias*/ 0.0,
1184 },
1185 /* `arcminutes` / `arcseconds` are used in Astronomy/Navigation areas. */
1186 {
1187 /*name*/ "arcminute",
1188 /*name_plural*/ "arcminutes",
1189 /*name_short*/ "'",
1190 /*name_alt*/ "amin",
1191 /*name_display*/ "Arcminutes",
1192 /*identifier*/ "ARCMINUTES",
1193 /*scalar*/ (M_PI / 180.0) / 60.0,
1194 /*bias*/ 0.0,
1196 },
1197 {
1198 /*name*/ "arcsecond",
1199 /*name_plural*/ "arcseconds",
1200 /*name_short*/ "\"",
1201 /*name_alt*/ "asec",
1202 /*name_display*/ "Arcseconds",
1203 /*identifier*/ "ARCSECONDS",
1204 /*scalar*/ (M_PI / 180.0) / 3600.0,
1205 /*bias*/ 0.0,
1207 },
1208 {
1209 /*name*/ "radian",
1210 /*name_plural*/ "radians",
1211 /*name_short*/ "rad",
1212 /*name_alt*/ "r",
1213 /*name_display*/ "Radians",
1214 /*identifier*/ "RADIANS",
1215 /*scalar*/ 1.0,
1216 /*bias*/ 0.0,
1217 /*flag*/ B_UNIT_DEF_NONE,
1218 },
1219#if 0
1220 {
1221 /*name*/ "turn",
1222 /*name_plural*/ "turns",
1223 /*name_short*/ "t",
1224 /*name_alt*/ nullptr,
1225 /*name_display*/ "Turns",
1226 /*identifier*/ nullptr,
1227 /*scalar*/ 1.0 / (M_PI * 2.0),
1228 /*bias*/ 0.0,
1229 /*flag*/ B_UNIT_DEF_NONE,
1230 },
1231#endif
1232 NULL_UNIT,
1233};
1235 /*units*/ buNaturalRotDef,
1236 /*base_unit*/ 0,
1237 /*flag*/ 0,
1239};
1240
1241/* Camera Lengths. */
1243 /* Base unit. */
1244 {
1245 /*name*/ "meter",
1246 /*name_plural*/ "meters",
1247 /*name_short*/ "m",
1248 /*name_alt*/ nullptr,
1249 /*name_display*/ "Meters",
1250 /*identifier*/ nullptr,
1251 /*scalar*/ UN_SC_KM,
1252 /*bias*/ 0.0,
1253 /*flag*/ B_UNIT_DEF_NONE,
1254 },
1255 {
1256 /*name*/ "decimeter",
1257 /*name_plural*/ "decimeters",
1258 /*name_short*/ "dm",
1259 /*name_alt*/ nullptr,
1260 /*name_display*/ "10 Centimeters",
1261 /*identifier*/ nullptr,
1262 /*scalar*/ UN_SC_HM,
1263 /*bias*/ 0.0,
1264 /*flag*/ B_UNIT_DEF_SUPPRESS,
1265 },
1266 {
1267 /*name*/ "centimeter",
1268 /*name_plural*/ "centimeters",
1269 /*name_short*/ "cm",
1270 /*name_alt*/ nullptr,
1271 /*name_display*/ "Centimeters",
1272 /*identifier*/ nullptr,
1273 /*scalar*/ UN_SC_DAM,
1274 /*bias*/ 0.0,
1275 /*flag*/ B_UNIT_DEF_SUPPRESS,
1276 },
1277 {
1278 /*name*/ "millimeter",
1279 /*name_plural*/ "millimeters",
1280 /*name_short*/ "mm",
1281 /*name_alt*/ nullptr,
1282 /*name_display*/ "Millimeters",
1283 /*identifier*/ nullptr,
1284 /*scalar*/ UN_SC_M,
1285 /*bias*/ 0.0,
1286 /*flag*/ B_UNIT_DEF_NONE,
1287 },
1288 {
1289 /*name*/ "micrometer",
1290 /*name_plural*/ "micrometers",
1291 /*name_short*/ "µm",
1292 /*name_alt*/ "um",
1293 /*name_display*/ "Micrometers",
1294 /*identifier*/ nullptr,
1295 /*scalar*/ UN_SC_MM,
1296 /*bias*/ 0.0,
1297 /*flag*/ B_UNIT_DEF_SUPPRESS,
1298 },
1299 NULL_UNIT,
1300};
1302 /*units*/ buCameraLenDef,
1303 /*base_unit*/ 3,
1304 /*flag*/ 0,
1306};
1307
1308/* (Light) Power. */
1310 {
1311 /*name*/ "gigawatt",
1312 /*name_plural*/ "gigawatts",
1313 /*name_short*/ "GW",
1314 /*name_alt*/ nullptr,
1315 /*name_display*/ "Gigawatts",
1316 /*identifier*/ nullptr,
1317 /*scalar*/ 1e9f,
1318 /*bias*/ 0.0,
1319 /*flag*/ B_UNIT_DEF_NONE,
1320 },
1321 {
1322 /*name*/ "megawatt",
1323 /*name_plural*/ "megawatts",
1324 /*name_short*/ "MW",
1325 /*name_alt*/ nullptr,
1326 /*name_display*/ "Megawatts",
1327 /*identifier*/ nullptr,
1328 /*scalar*/ 1e6f,
1329 /*bias*/ 0.0,
1331 },
1332 {
1333 /*name*/ "kilowatt",
1334 /*name_plural*/ "kilowatts",
1335 /*name_short*/ "kW",
1336 /*name_alt*/ nullptr,
1337 /*name_display*/ "Kilowatts",
1338 /*identifier*/ nullptr,
1339 /*scalar*/ 1e3f,
1340 /*bias*/ 0.0,
1341 /*flag*/ B_UNIT_DEF_SUPPRESS,
1342 },
1343 /* Base unit. */
1344 {
1345 /*name*/ "watt",
1346 /*name_plural*/ "watts",
1347 /*name_short*/ "W",
1348 /*name_alt*/ nullptr,
1349 /*name_display*/ "Watts",
1350 /*identifier*/ nullptr,
1351 /*scalar*/ 1.0f,
1352 /*bias*/ 0.0,
1353 /*flag*/ B_UNIT_DEF_NONE,
1354 },
1355 {
1356 /*name*/ "milliwatt",
1357 /*name_plural*/ "milliwatts",
1358 /*name_short*/ "mW",
1359 /*name_alt*/ nullptr,
1360 /*name_display*/ "Milliwatts",
1361 /*identifier*/ nullptr,
1362 /*scalar*/ 1e-3f,
1363 /*bias*/ 0.0,
1365 },
1366 {
1367 /*name*/ "microwatt",
1368 /*name_plural*/ "microwatts",
1369 /*name_short*/ "µW",
1370 /*name_alt*/ "uW",
1371 /*name_display*/ "Microwatts",
1372 /*identifier*/ nullptr,
1373 /*scalar*/ 1e-6f,
1374 /*bias*/ 0.0,
1375 /*flag*/ B_UNIT_DEF_NONE,
1376 },
1377 {
1378 /*name*/ "nanowatt",
1379 /*name_plural*/ "nanowatts",
1380 /*name_short*/ "nW",
1381 /*name_alt*/ nullptr,
1382 /*name_display*/ "Nanowatts",
1383 /*identifier*/ nullptr,
1384 /*scalar*/ 1e-9f,
1385 /*bias*/ 0.0,
1386 /*flag*/ B_UNIT_DEF_NONE,
1387 },
1388 NULL_UNIT,
1389};
1391 /*units*/ buPowerDef,
1392 /*base_unit*/ 3,
1393 /*flag*/ 0,
1395};
1396
1397/* Temperature */
1399 {
1400 /*name*/ "kelvin",
1401 /*name_plural*/ "kelvin",
1402 /*name_short*/ "K",
1403 /*name_alt*/ nullptr,
1404 /*name_display*/ "Kelvin",
1405 /*identifier*/ "KELVIN",
1406 /*scalar*/ 1.0f,
1407 /*bias*/ 0.0,
1408 /*flag*/ B_UNIT_DEF_NONE,
1409 },
1410 /* Base unit. */
1411 {
1412 /*name*/ "celsius",
1413 /*name_plural*/ "celsius",
1414 /*name_short*/ BLI_STR_UTF8_DEGREE_SIGN "C",
1415 /*name_alt*/ "C",
1416 /*name_display*/ "Celsius",
1417 /*identifier*/ "CELSIUS",
1418 /*scalar*/ 1.0f,
1419 /*bias*/ 273.15,
1420 /*flag*/ B_UNIT_DEF_NONE,
1421 },
1422 NULL_UNIT,
1423};
1425 /*units*/ buMetricTempDef,
1426 /*base_unit*/ 0,
1427 /*flag*/ 0,
1429};
1430
1432 {
1433 /*name*/ "kelvin",
1434 /*name_plural*/ "kelvin",
1435 /*name_short*/ "K",
1436 /*name_alt*/ nullptr,
1437 /*name_display*/ "Kelvin",
1438 /*identifier*/ "KELVIN",
1439 /*scalar*/ 1.0f,
1440 /*bias*/ 0.0,
1441 /*flag*/ B_UNIT_DEF_NONE,
1442 },
1443 /* Base unit. */
1444 {
1445 /*name*/ "fahrenheit",
1446 /*name_plural*/ "fahrenheit",
1447 /*name_short*/ BLI_STR_UTF8_DEGREE_SIGN "F",
1448 /*name_alt*/ "F",
1449 /*name_display*/ "Fahrenheit",
1450 /*identifier*/ "FAHRENHEIT",
1451 /*scalar*/ UN_SC_FAH,
1452 /*bias*/ 459.67,
1453 /*flag*/ B_UNIT_DEF_NONE,
1454 },
1455 NULL_UNIT,
1456};
1458 /*units*/ buImperialTempDef,
1459 /*base_unit*/ 1,
1460 /*flag*/ 0,
1462};
1463
1464/* Color Temperature */
1466 /* Base unit. */
1467 {
1468 /*name*/ "kelvin",
1469 /*name_plural*/ "kelvin",
1470 /*name_short*/ "K",
1471 /*name_alt*/ nullptr,
1472 /*name_display*/ "Kelvin",
1473 /*identifier*/ "KELVIN",
1474 /*scalar*/ 1.0f,
1475 /*bias*/ 0.0,
1476 /*flag*/ B_UNIT_DEF_NONE,
1477 },
1478 NULL_UNIT,
1479};
1481 /*units*/ buColorTempDef,
1482 /*base_unit*/ 0,
1483 /*flag*/ 0,
1485};
1486
1487/* Frequency */
1489 /* Base unit. */
1490 {
1491 /*name*/ "hertz",
1492 /*name_plural*/ "hertz",
1493 /*name_short*/ "Hz",
1494 /*name_alt*/ nullptr,
1495 /*name_display*/ "Hertz",
1496 /*identifier*/ "HERTZ",
1497 /*scalar*/ 1.0f,
1498 /*bias*/ 0.0,
1499 /*flag*/ B_UNIT_DEF_NONE,
1500 },
1501 {
1502 /*name*/ "kilohertz",
1503 /*name_plural*/ "kilohertz",
1504 /*name_short*/ "kHz",
1505 /*name_alt*/ nullptr,
1506 /*name_display*/ "Kilohertz",
1507 /*identifier*/ "KILOHERTZ",
1508 /*scalar*/ 1e3f,
1509 /*bias*/ 0.0,
1510 /*flag*/ B_UNIT_DEF_NONE,
1511 },
1512 NULL_UNIT,
1513};
1515 /*units*/ buFrequencyDef,
1516 /*base_unit*/ 0,
1517 /*flag*/ 0,
1519};
1520
1521#define UNIT_SYSTEM_TOT (((sizeof(bUnitSystems) / B_UNIT_TYPE_TOT) / sizeof(void *)) - 1)
1523 /* Natural. */
1524 {
1525 /*B_UNIT_NONE*/ nullptr,
1526 /*B_UNIT_LENGTH*/ nullptr,
1527 /*B_UNIT_AREA*/ nullptr,
1528 /*B_UNIT_VOLUME*/ nullptr,
1529 /*B_UNIT_MASS*/ nullptr,
1530 /*B_UNIT_ROTATION*/ &buNaturalRotCollection,
1531 /*B_UNIT_TIME*/ &buNaturalTimeCollection,
1532 /*B_UNIT_TIME_ABSOLUTE*/ &buNaturalTimeCollection,
1533 /*B_UNIT_VELOCITY*/ nullptr,
1534 /*B_UNIT_ACCELERATION*/ nullptr,
1535 /*B_UNIT_CAMERA*/ nullptr,
1536 /*B_UNIT_POWER*/ nullptr,
1537 /*B_UNIT_TEMPERATURE*/ nullptr,
1538 /*B_UNIT_WAVELENGTH*/ nullptr,
1539 /*B_UNIT_COLOR_TEMPERATURE*/ nullptr,
1540 /*B_UNIT_FREQUENCY*/ nullptr,
1541 },
1542 /* Metric. */
1543 {
1544 /*B_UNIT_NONE*/ nullptr,
1545 /*B_UNIT_LENGTH*/ &buMetricLenCollection,
1546 /*B_UNIT_AREA*/ &buMetricAreaCollection,
1547 /*B_UNIT_VOLUME*/ &buMetricVolCollection,
1548 /*B_UNIT_MASS*/ &buMetricMassCollection,
1549 /*B_UNIT_ROTATION*/ &buNaturalRotCollection,
1550 /*B_UNIT_TIME*/ &buNaturalTimeCollection,
1551 /*B_UNIT_TIME_ABSOLUTE*/ &buNaturalTimeCollection,
1552 /*B_UNIT_VELOCITY*/ &buMetricVelCollection,
1553 /*B_UNIT_ACCELERATION*/ &buMetricAclCollection,
1554 /*B_UNIT_CAMERA*/ &buCameraLenCollection,
1555 /*B_UNIT_POWER*/ &buPowerCollection,
1556 /*B_UNIT_TEMPERATURE*/ &buMetricTempCollection,
1557 /*B_UNIT_WAVELENGTH*/ &buWavelengthLenCollection,
1558 /*B_UNIT_COLOR_TEMPERATURE*/ &buColorTempCollection,
1559 /*B_UNIT_FREQUENCY*/ &buFrequencyCollection,
1560 },
1561 /* Imperial. */
1562 {
1563 /*B_UNIT_NONE*/ nullptr,
1564 /*B_UNIT_LENGTH*/ &buImperialLenCollection,
1565 /*B_UNIT_AREA*/ &buImperialAreaCollection,
1566 /*B_UNIT_VOLUME*/ &buImperialVolCollection,
1567 /*B_UNIT_MASS*/ &buImperialMassCollection,
1568 /*B_UNIT_ROTATION*/ &buNaturalRotCollection,
1569 /*B_UNIT_TIME*/ &buNaturalTimeCollection,
1570 /*B_UNIT_TIME_ABSOLUTE*/ &buNaturalTimeCollection,
1571 /*B_UNIT_VELOCITY*/ &buImperialVelCollection,
1572 /*B_UNIT_ACCELERATION*/ &buImperialAclCollection,
1573 /*B_UNIT_CAMERA*/ &buCameraLenCollection,
1574 /*B_UNIT_POWER*/ &buPowerCollection,
1575 /*B_UNIT_TEMPERATURE*/ &buImperialTempCollection,
1576 /*B_UNIT_WAVELENGTH*/ &buWavelengthLenCollection,
1577 /*B_UNIT_COLOR_TEMPERATURE*/ &buColorTempCollection,
1578 /*B_UNIT_FREQUENCY*/ &buFrequencyCollection,
1579 },
1580 {nullptr},
1581};
1582
1583static const bUnitCollection *unit_get_system(int system, int type)
1584{
1585 BLI_assert((system > -1) && (system < UNIT_SYSTEM_TOT) && (type > -1) &&
1586 (type < B_UNIT_TYPE_TOT));
1587 return bUnitSystems[system][type]; /* Select system to use: metric/imperial/other? */
1588}
1589
1590static const bUnitDef *unit_default(const bUnitCollection *usys)
1591{
1592 return &usys->units[usys->base_unit];
1593}
1594
1595static const bUnitDef *unit_best_fit(double value,
1596 const bUnitCollection *usys,
1597 const bUnitDef *unit_start,
1598 int suppress)
1599{
1600 double value_abs = value > 0.0 ? value : -value;
1601
1602 for (const bUnitDef *unit = unit_start ? unit_start : usys->units; unit->name; unit++) {
1603
1604 if (suppress && (unit->flag & B_UNIT_DEF_SUPPRESS)) {
1605 continue;
1606 }
1607
1608 /* Scale down scalar so 1cm doesn't convert to 10mm because of float error. */
1609 if (unit->flag & B_UNIT_DEF_TENTH) [[unlikely]] {
1610 if (value_abs >= unit->scalar * (0.1 - EPS)) {
1611 return unit;
1612 }
1613 }
1614 else {
1615 if (value_abs >= unit->scalar * (1.0 - EPS)) {
1616 return unit;
1617 }
1618 }
1619 }
1620
1621 return unit_default(usys);
1622}
1623
1624/* Convert into 2 units and 2 values for "2ft, 3inch" syntax. */
1625static void unit_dual_convert(double value,
1626 const bUnitCollection *usys,
1627 bUnitDef const **r_unit_a,
1628 bUnitDef const **r_unit_b,
1629 double *r_value_a,
1630 double *r_value_b,
1631 const bUnitDef *main_unit)
1632{
1633 const bUnitDef *unit = (main_unit) ? main_unit : unit_best_fit(value, usys, nullptr, 1);
1634
1635 const double scaled_value = value / unit->scalar;
1636 *r_value_a = std::trunc(scaled_value) * unit->scalar;
1637 *r_value_b = value - (*r_value_a);
1638
1639 *r_unit_a = unit;
1640 *r_unit_b = unit_best_fit(*r_value_b, usys, *r_unit_a, 1);
1641}
1642
1643static size_t unit_as_string(char *str,
1644 int str_maxncpy,
1645 double value,
1646 int prec,
1647 const bool variable_width,
1648 const bUnitCollection *usys,
1649 /* Non exposed options. */
1650 const bUnitDef *unit,
1651 char pad,
1652 bool do_unit_suffix)
1653{
1654 BLI_assert(prec >= 0);
1655 if (unit == nullptr) {
1656 if (value == 0.0) {
1657 /* Use the default units since there is no way to convert. */
1658 unit = unit_default(usys);
1659 }
1660 else {
1661 unit = unit_best_fit(value, usys, nullptr, 1);
1662 }
1663 }
1664
1665 double value_conv = (value / unit->scalar) - unit->bias;
1666
1667 /* Adjust precision to expected number of significant digits.
1668 * Note that here, we shall not have to worry about very big/small numbers, units are expected
1669 * to replace 'scientific notation' in those cases.
1670 * Fixed width mode skips this to preserve the exact decimal place count. */
1671 if (variable_width) {
1672 prec -= integer_digits_d(value_conv);
1673 }
1674
1675 CLAMP(prec, 0, 6);
1676
1677 /* Convert to a string. */
1678 size_t len = BLI_snprintf_rlen(str, str_maxncpy, "%.*f", prec, value_conv);
1679
1680 /* Add unit prefix and strip zeros. */
1681
1682 /* Replace trailing zero's with spaces so the number
1683 * is less complicated but alignment in a button won't
1684 * jump about while dragging. */
1685 size_t i = len - 1;
1686
1687 if (prec > 0) {
1688 if (variable_width) {
1689 while (i > 0 && str[i] == '0') { /* 4.300 -> 4.3 */
1690 str[i--] = pad;
1691 }
1692
1693 if (i > 0 && str[i] == '.') { /* 10. -> 10 */
1694 str[i--] = pad;
1695 }
1696 }
1697 }
1698
1699 /* Now add a space for all units except foot, inch, degree, arcminute, arcsecond. */
1700 if (!(unit->flag & B_UNIT_DEF_NO_SPACE) && do_unit_suffix) {
1701 str[++i] = ' ';
1702 }
1703
1704 if (i < str_maxncpy) {
1705 i++;
1706 if (do_unit_suffix) {
1707 /* Now add the suffix. */
1708 int j = 0;
1709 while (unit->name_short[j] && (i < str_maxncpy)) {
1710 str[i++] = unit->name_short[j++];
1711 }
1712 }
1713 }
1714
1715 /* Terminate no matter what's done with padding above. */
1716 if (i >= str_maxncpy) {
1717 i = str_maxncpy - 1;
1718 }
1719
1720 str[i] = '\0';
1721 return i;
1722}
1723
1724static bool unit_should_be_split(int type)
1725{
1727}
1728
1732 /* USER_UNIT_ADAPTIVE means none, otherwise the value is the index in the collection. */
1734 int mass;
1735 int time;
1737};
1738
1740{
1741 PreferredUnits units = {0};
1742 units.system = settings.system;
1743 units.rotation = settings.system_rotation;
1744 units.length = settings.length_unit;
1745 units.mass = settings.mass_unit;
1746 units.time = settings.time_unit;
1747 units.temperature = settings.temperature_unit;
1748 return units;
1749}
1750
1751static size_t unit_as_string_split_pair(char *str,
1752 int str_maxncpy,
1753 double value,
1754 int prec,
1755 const bool variable_width,
1756 const bUnitCollection *usys,
1757 const bUnitDef *main_unit)
1758{
1759 BLI_assert(prec >= 0);
1760 const bUnitDef *unit_a, *unit_b;
1761 double value_a, value_b;
1762 unit_dual_convert(value, usys, &unit_a, &unit_b, &value_a, &value_b, main_unit);
1763
1764 /* Check the 2 is a smaller unit. */
1765 if (unit_b > unit_a) {
1766 /* Always strip zeros for the larger unit, since it is truncated and won't ever "jitter". */
1767 size_t i = unit_as_string(str, str_maxncpy, value_a, prec, true, usys, unit_a, '\0', true);
1768
1769 /* Fixed width mode skips this to preserve the exact decimal place count. */
1770 if (variable_width) {
1771 prec -= integer_digits_d(value_a / unit_b->scalar) -
1772 integer_digits_d(value_b / unit_b->scalar);
1773 prec = max_ii(prec, 0);
1774 }
1775
1776 /* Is there enough space for at least 1 char of the next unit? */
1777 if (i + 2 < str_maxncpy) {
1778 str[i++] = ' ';
1779
1780 /* Use low precision since this is a smaller unit. */
1781 i += unit_as_string(
1782 str + i, str_maxncpy - i, value_b, prec, variable_width, usys, unit_b, '\0', true);
1783 }
1784 return i;
1785 }
1786
1787 return -1;
1788}
1789
1791{
1792 return usys != nullptr && usys->units[0].name != nullptr;
1793}
1794
1795static bool is_unit_adaptive(const int type, const PreferredUnits &units)
1796{
1797 switch (type) {
1798 case B_UNIT_LENGTH:
1799 case B_UNIT_AREA:
1800 case B_UNIT_VOLUME:
1801 return units.length == USER_UNIT_ADAPTIVE;
1802 case B_UNIT_MASS:
1803 return units.mass == USER_UNIT_ADAPTIVE;
1804 case B_UNIT_TIME:
1805 return units.time == USER_UNIT_ADAPTIVE;
1806 case B_UNIT_TEMPERATURE:
1807 return units.temperature == USER_UNIT_ADAPTIVE;
1808 default:
1809 break;
1810 }
1811 return false;
1812}
1813
1815{
1816 const bUnitCollection *usys = unit_get_system(units.system, type);
1817 if (!is_valid_unit_collection(usys)) {
1818 return -1;
1819 }
1820 if (is_unit_adaptive(type, units)) {
1821 return -1;
1822 }
1823
1824 int max_offset = usys->length - 1;
1825
1826 switch (type) {
1827 case B_UNIT_LENGTH:
1828 case B_UNIT_AREA:
1829 case B_UNIT_VOLUME:
1830 return std::min(units.length, max_offset);
1831 case B_UNIT_MASS:
1832 return std::min(units.mass, max_offset);
1833 case B_UNIT_TIME:
1834 return std::min(units.time, max_offset);
1835 case B_UNIT_ROTATION:
1836 if (units.rotation == 0) {
1837 return 0;
1838 }
1839 else if (units.rotation == USER_UNIT_ROT_RADIANS) {
1840 return 3;
1841 }
1842 break;
1843 case B_UNIT_TEMPERATURE:
1844 return std::min(units.temperature, max_offset);
1845 default:
1846 break;
1847 }
1848 return -1;
1849}
1850
1852{
1853 const bUnitCollection *usys = unit_get_system(units.system, type);
1854 const int index = get_preferred_display_unit_index_if_used(type, units);
1855 if (index == -1) {
1856 return nullptr;
1857 }
1858 return &usys->units[index];
1859}
1860
1861/* Return the length of the generated string. */
1862static size_t unit_as_string_main(char *str,
1863 int str_maxncpy,
1864 double value,
1865 int prec,
1866 int type,
1867 bool split,
1868 bool pad,
1869 bool do_unit_suffix,
1870 const PreferredUnits &units)
1871{
1872 const bUnitCollection *usys = unit_get_system(units.system, type);
1873 const bUnitDef *main_unit = nullptr;
1874
1875 if (!is_valid_unit_collection(usys)) {
1876 usys = &buDummyCollection;
1877 }
1878 else {
1879 main_unit = get_preferred_display_unit_if_used(type, units);
1880 }
1881
1882 bool variable_width = true;
1883 if (prec < 0) {
1884 prec = -prec;
1885 variable_width = false;
1886 }
1887
1888 if (split && unit_should_be_split(type)) {
1890 str, str_maxncpy, value, prec, variable_width, usys, main_unit);
1891 /* Split failed when length is negative, fall back to no split. */
1892 if (length >= 0) {
1893 return length;
1894 }
1895 }
1896
1897 return unit_as_string(str,
1898 str_maxncpy,
1899 value,
1900 prec,
1901 variable_width,
1902 usys,
1903 main_unit,
1904 pad ? ' ' : '\0',
1905 do_unit_suffix);
1906}
1907
1909 int str_maxncpy,
1910 double value,
1911 int prec,
1912 int system,
1913 int type,
1914 bool split,
1915 bool pad,
1916 bool do_unit_suffix)
1917{
1918 PreferredUnits units;
1919 units.system = system;
1920 units.rotation = 0;
1921 units.length = USER_UNIT_ADAPTIVE;
1922 units.mass = USER_UNIT_ADAPTIVE;
1923 units.time = USER_UNIT_ADAPTIVE;
1925 return unit_as_string_main(
1926 str, str_maxncpy, value, prec, type, split, pad, do_unit_suffix, units);
1927}
1928
1930 int str_maxncpy,
1931 double value,
1932 int prec,
1933 int type,
1934 const UnitSettings &settings,
1935 bool pad,
1936 bool do_unit_suffix)
1937{
1938 bool do_split = (settings.flag & USER_UNIT_OPT_SPLIT) != 0;
1940 return unit_as_string_main(
1941 str, str_maxncpy, value, prec, type, do_split, pad, do_unit_suffix, units);
1942}
1943
1945 int str_maxncpy,
1946 double value,
1947 int prec,
1948 int type,
1949 const UnitSettings &settings,
1950 bool pad,
1951 bool do_unit_suffix)
1952{
1954 str_maxncpy,
1955 BKE_unit_value_scale(settings, type, value),
1956 prec,
1957 type,
1958 settings,
1959 pad,
1960 do_unit_suffix);
1961}
1962
1963double BKE_unit_value_scale(const UnitSettings &settings, const int unit_type, double value)
1964{
1965 if (settings.system == USER_UNIT_NONE) {
1966 /* Never apply scale_length when not using a unit setting! */
1967 return value;
1968 }
1969
1970 switch (unit_type) {
1971 case B_UNIT_LENGTH:
1972 case B_UNIT_VELOCITY:
1974 return value * double(settings.scale_length);
1975 case B_UNIT_AREA:
1976 case B_UNIT_POWER:
1977 return value * pow(settings.scale_length, 2);
1978 case B_UNIT_VOLUME:
1979 return value * pow(settings.scale_length, 3);
1980 case B_UNIT_MASS:
1981 return value * pow(settings.scale_length, 3);
1982 case B_UNIT_CAMERA: /* *Do not* use scene's unit scale for camera focal lens! See #42026. */
1983 case B_UNIT_WAVELENGTH: /* Wavelength values are independent of the scene scale. */
1984 default:
1985 return value;
1986 }
1987}
1988
1989BLI_INLINE bool isalpha_or_utf8(const int ch)
1990{
1991 return (ch >= 128 || isalpha(ch));
1992}
1993
1994static const char *unit_find_str(const char *str, const char *substr, bool case_sensitive)
1995{
1996 if (substr == nullptr || substr[0] == '\0') {
1997 return nullptr;
1998 }
1999
2000 while (true) {
2001 /* Unit detection is case insensitive. */
2002 const char *str_found;
2003 if (case_sensitive) {
2004 str_found = strstr(str, substr);
2005 }
2006 else {
2007 str_found = BLI_strcasestr(str, substr);
2008 }
2009
2010 if (str_found) {
2011 /* Previous char cannot be a letter. */
2012 if (str_found == str ||
2013 /* Weak unicode support!, so "µm" won't match up be replaced by "m"
2014 * since non ASCII UTF8 values will NEVER return true. */
2016 {
2017 /* Next char cannot be alphanumeric. */
2018 int len_name = strlen(substr);
2019
2020 if (!isalpha_or_utf8(*(str_found + len_name))) {
2021 return str_found;
2022 }
2023 }
2024 /* If str_found is not a valid unit, we have to check further in the string... */
2025 for (str_found++; isalpha_or_utf8(*str_found); str_found++) {
2026 /* Pass. */
2027 }
2028 str = str_found;
2029 }
2030 else {
2031 break;
2032 }
2033 }
2034
2035 return nullptr;
2036}
2037
2038/* Note that numbers are added within brackets.
2039 * ") " - is used to detect numbers we added so we can detect if commas need to be added.
2040 *
2041 * "1m1cm+2mm" - Original value.
2042 * "1*1#1*0.01#+2*0.001#" - Replace numbers.
2043 * "1*1+1*0.01 +2*0.001 " - Add plus signs if ( + - * / | & ~ < > ^ ! = % ) not found in between.
2044 */
2045
2046/* Not too strict, (+ - * /) are most common. */
2047static bool ch_is_op(char op)
2048{
2049 switch (op) {
2050 case '+':
2051 case '-':
2052 case '*':
2053 case '/':
2054 case '|':
2055 case '&':
2056 case '~':
2057 case '<':
2058 case '>':
2059 case '^':
2060 case '!':
2061 case '=':
2062 case '%':
2063 return true;
2064 default:
2065 return false;
2066 }
2067}
2068
2069static bool ch_is_op_unary(char op)
2070{
2071 switch (op) {
2072 case '+':
2073 case '-':
2074 case '~':
2075 return true;
2076 default:
2077 return false;
2078 }
2079}
2080
2087static const char *find_next_negative(const char *str, const char *remaining_str)
2088{
2089 const char *str_found = strstr(remaining_str, "-");
2090
2091 if (str_found == nullptr) {
2092 return nullptr;
2093 }
2094
2095 /* Don't use the "-" from scientific notation, but make sure we can look backwards first. */
2096 if ((str_found != str) && ELEM(*(str_found - 1), 'e', 'E')) {
2097 return find_next_negative(str, str_found + 1);
2098 }
2099
2100 if (*(str_found + 1) == ' ') {
2101 str_found++;
2102 }
2103
2104 return str_found + 1;
2105}
2106
2113static char *find_next_op(const char *str, char *remaining_str, int remaining_str_maxncpy)
2114{
2115 int i;
2116 for (i = 0; i < remaining_str_maxncpy; i++) {
2117 if (remaining_str[i] == '\0') {
2118 return remaining_str + i;
2119 }
2120
2121 if (ch_is_op(remaining_str[i])) {
2122 /* Make sure we don't look backwards before the start of the string. */
2123 if (remaining_str != str && i != 0) {
2124 /* Check for velocity or acceleration (e.g. '/' in 'ft/s' is not an op). */
2125 if ((remaining_str[i] == '/') && ELEM(remaining_str[i - 1], 't', 'T', 'm', 'M') &&
2126 ELEM(remaining_str[i + 1], 's', 'S'))
2127 {
2128 continue;
2129 }
2130
2131 /* Check for scientific notation. */
2132 if (ELEM(remaining_str[i - 1], 'e', 'E')) {
2133 continue;
2134 }
2135
2136 /* Return position before a space character. */
2137 if (remaining_str[i - 1] == ' ') {
2138 i--;
2139 }
2140 }
2141
2142 return remaining_str + i;
2143 }
2144 }
2145 BLI_assert_msg(0, "String should be null terminated");
2146 return remaining_str + i;
2147}
2148
2153static char *skip_unary_op(char *str)
2154{
2155 while (*str == ' ' || ch_is_op_unary(*str)) {
2156 str++;
2157 }
2158 return str;
2159}
2160
2167static bool unit_distribute_negatives(char *str, const int str_maxncpy)
2168{
2169 bool changed = false;
2170
2171 char *remaining_str = str;
2172 while ((remaining_str = const_cast<char *>(find_next_negative(str, remaining_str))) != nullptr) {
2173 int remaining_str_maxncpy;
2174 /* Exit early in the unlikely situation that we've run out of length to add the parentheses. */
2175 remaining_str_maxncpy = str_maxncpy - int(remaining_str - str);
2176 if (remaining_str_maxncpy <= 2) {
2177 return changed;
2178 }
2179
2180 changed = true;
2181
2182 /* Add `(`, shift the following characters to the right to make space. */
2183 memmove(remaining_str + 1, remaining_str, remaining_str_maxncpy - 2);
2184 *remaining_str = '(';
2185
2186 /* Add the `)` before the next operation or at the end.
2187 * Unary operators are skipped to allow `--` to be a supported prefix. */
2188 remaining_str = find_next_op(str, skip_unary_op(remaining_str + 1), remaining_str_maxncpy);
2189 remaining_str_maxncpy = str_maxncpy - int(remaining_str - str);
2190 memmove(remaining_str + 1, remaining_str, remaining_str_maxncpy - 2);
2191 *remaining_str = ')';
2192
2193 /* Only move forward by 1 even though we added two characters. Minus signs need to be able to
2194 * apply to the next block of values too. */
2195 remaining_str += 1;
2196 }
2197
2198 return changed;
2199}
2200
2205static int find_previous_non_value_char(const char *str, const int start_ofs)
2206{
2207 for (int i = start_ofs; i > 0; i--) {
2208 if (ch_is_op(str[i - 1]) || strchr("( )", str[i - 1])) {
2209 return i;
2210 }
2211 }
2212 return 0;
2213}
2214
2219static int find_end_of_value_chars(const char *str, const int str_maxncpy, const int start_ofs)
2220{
2221 int i;
2222 for (i = start_ofs; i < str_maxncpy; i++) {
2223 if (!strchr("0123456789eE.", str[i])) {
2224 return i;
2225 }
2226 }
2227 return i;
2228}
2229
2230static int unit_scale_str(char *str,
2231 int str_maxncpy,
2232 char *str_tmp,
2233 double scale_pref,
2234 const bUnitDef *unit,
2235 const char *replace_str,
2236 bool case_sensitive)
2237{
2238 if (str_maxncpy < 0) {
2239 return 0;
2240 }
2241
2242 /* XXX: investigate, does not respect str_maxncpy properly. */
2243 char *str_found = const_cast<char *>(unit_find_str(str, replace_str, case_sensitive));
2244
2245 if (str_found == nullptr) {
2246 return 0;
2247 }
2248
2249 int found_ofs = int(str_found - str);
2250
2251 int len = strlen(str);
2252
2253 /* Deal with unit bias for temperature units. Order of operations is important, so we
2254 * have to add parentheses, add the bias, then multiply by the scalar like usual.
2255 *
2256 * NOTE: If these changes don't fit in the buffer properly unit evaluation has failed,
2257 * just try not to destroy anything while failing. */
2258 if (unit->bias != 0.0) {
2259 /* Add the open parenthesis. */
2260 int prev_op_ofs = find_previous_non_value_char(str, found_ofs);
2261 if (len + 1 < str_maxncpy) {
2262 memmove(str + prev_op_ofs + 1, str + prev_op_ofs, len - prev_op_ofs + 1);
2263 str[prev_op_ofs] = '(';
2264 len++;
2265 found_ofs++;
2266 str_found++;
2267 } /* If this doesn't fit, we have failed. */
2268
2269 /* Add the addition sign, the bias, and the close parenthesis after the value. */
2270 int value_end_ofs = find_end_of_value_chars(str, str_maxncpy, prev_op_ofs + 2);
2271 value_end_ofs = std::min(value_end_ofs, found_ofs);
2272 int len_bias_num = BLI_snprintf_rlen(str_tmp, TEMP_STR_SIZE, "+%.9g)", unit->bias);
2273 if (value_end_ofs + len_bias_num < str_maxncpy) {
2274 memmove(str + value_end_ofs + len_bias_num, str + value_end_ofs, len - value_end_ofs + 1);
2275 memcpy(str + value_end_ofs, str_tmp, len_bias_num);
2276 len += len_bias_num;
2277 found_ofs += len_bias_num;
2278 str_found += len_bias_num;
2279 } /* If this doesn't fit, we have failed. */
2280 }
2281
2282 int len_name = strlen(replace_str);
2283 int len_move = (len - (found_ofs + len_name)) + 1; /* 1+ to copy the string terminator. */
2284
2285 /* "#" Removed later */
2286 int len_num = BLI_snprintf_rlen(
2287 str_tmp, TEMP_STR_SIZE, "*%.9g" SEP_STR, unit->scalar / scale_pref);
2288
2289 len_num = std::min(len_num, str_maxncpy);
2290
2291 if (found_ofs + len_num + len_move > str_maxncpy) {
2292 /* Can't move the whole string, move just as much as will fit. */
2293 len_move -= (found_ofs + len_num + len_move) - str_maxncpy;
2294 }
2295
2296 if (len_move > 0) {
2297 /* Resize the last part of the string.
2298 * May grow or shrink the string. */
2299 memmove(str_found + len_num, str_found + len_name, len_move);
2300 }
2301
2302 if (found_ofs + len_num > str_maxncpy) {
2303 /* Not even the number will fit into the string, only copy part of it. */
2304 len_num -= (found_ofs + len_num) - str_maxncpy;
2305 }
2306
2307 if (len_num > 0) {
2308 /* It's possible none of the number could be copied in. */
2309 memcpy(str_found, str_tmp, len_num); /* Without the string terminator. */
2310 }
2311
2312 /* Since the null terminator won't be moved if the stringlen_max
2313 * was not long enough to fit everything in it. */
2314 str[str_maxncpy - 1] = '\0';
2315 return found_ofs + len_num;
2316}
2317
2318static int unit_replace(
2319 char *str, int str_maxncpy, char *str_tmp, double scale_pref, const bUnitDef *unit)
2320{
2321 const bool case_sensitive = (unit->flag & B_UNIT_DEF_CASE_SENSITIVE) != 0;
2322 int ofs = 0;
2323 ofs += unit_scale_str(
2324 str + ofs, str_maxncpy - ofs, str_tmp, scale_pref, unit, unit->name_short, case_sensitive);
2325 ofs += unit_scale_str(
2326 str + ofs, str_maxncpy - ofs, str_tmp, scale_pref, unit, unit->name_plural, false);
2327 ofs += unit_scale_str(
2328 str + ofs, str_maxncpy - ofs, str_tmp, scale_pref, unit, unit->name_alt, case_sensitive);
2329 ofs += unit_scale_str(
2330 str + ofs, str_maxncpy - ofs, str_tmp, scale_pref, unit, unit->name, false);
2331 return ofs;
2332}
2333
2334static bool unit_find(const char *str, const bUnitDef *unit)
2335{
2336 const bool case_sensitive = (unit->flag & B_UNIT_DEF_CASE_SENSITIVE) != 0;
2337 if (unit_find_str(str, unit->name_short, case_sensitive)) {
2338 return true;
2339 }
2340 if (unit_find_str(str, unit->name_plural, false)) {
2341 return true;
2342 }
2343 if (unit_find_str(str, unit->name_alt, case_sensitive)) {
2344 return true;
2345 }
2346 if (unit_find_str(str, unit->name, false)) {
2347 return true;
2348 }
2349
2350 return false;
2351}
2352
2353static const bUnitDef *unit_find_in_collection(const bUnitCollection *usys, const char *str)
2354{
2355 for (const bUnitDef *unit = usys->units; unit->name; unit++) {
2356 if (unit_find(str, unit)) {
2357 return unit;
2358 }
2359 }
2360 return nullptr;
2361}
2362
2370 const char *str,
2371 const char *str_prev)
2372{
2373 /* See which units the new value has. */
2374 const bUnitDef *unit = unit_find_in_collection(usys, str);
2375 /* Else, try to infer the default unit from the previous string. */
2376 if (str_prev && (unit == nullptr)) {
2377 /* See which units the original value had. */
2378 unit = unit_find_in_collection(usys, str_prev);
2379 }
2380 /* Else, fall back to default unit. */
2381 if (unit == nullptr) {
2382 unit = unit_default(usys);
2383 }
2384
2385 return unit;
2386}
2387
2388bool BKE_unit_string_contains_unit(const char *str, int type)
2389{
2390 for (int system = 0; system < UNIT_SYSTEM_TOT; system++) {
2391 const bUnitCollection *usys = unit_get_system(system, type);
2392 if (!is_valid_unit_collection(usys)) {
2393 continue;
2394 }
2395 if (unit_find_in_collection(usys, str)) {
2396 return true;
2397 }
2398 }
2399 return false;
2400}
2401
2402double BKE_unit_apply_preferred_unit(const UnitSettings &settings, int type, double value)
2403{
2405 const bUnitDef *unit = get_preferred_display_unit_if_used(type, units);
2406
2407 const double scalar = (unit == nullptr) ? BKE_unit_base_scalar(units.system, type) :
2408 unit->scalar;
2409 const double bias = (unit == nullptr) ? 0.0 : unit->bias; /* Base unit shouldn't have a bias. */
2410
2411 return value * scalar + bias;
2412}
2413
2415 char *str, int str_maxncpy, const char *str_prev, double scale_pref, int system, int type)
2416{
2417 const bUnitCollection *usys = unit_get_system(system, type);
2418 if (!is_valid_unit_collection(usys)) {
2419 return false;
2420 }
2421
2422 double scale_pref_base = scale_pref;
2423 char str_tmp[TEMP_STR_SIZE];
2424 bool changed = false;
2425
2426 /* Fix cases like "-1m50cm" which would evaluate to -0.5m without this. */
2427 changed |= unit_distribute_negatives(str, str_maxncpy);
2428
2429 /* Try to find a default unit from current or previous string. */
2430 const bUnitDef *default_unit = unit_detect_from_str(usys, str, str_prev);
2431
2432 /* We apply the default unit to the whole expression (default unit is now the reference
2433 * '1.0' one). */
2434 scale_pref_base *= default_unit->scalar;
2435
2436 /* Apply the default unit on the whole expression, this allows to handle nasty cases like
2437 * '2+2in'. */
2438 if (SNPRINTF(str_tmp, "(%s)*%.9g", str, default_unit->scalar) < sizeof(str_tmp)) {
2439 BLI_strncpy(str, str_tmp, str_maxncpy);
2440 }
2441 else {
2442 /* BLI_snprintf would not fit into str_tmp, can't do much in this case.
2443 * Check for this because otherwise BKE_unit_replace_string could call itself forever. */
2444 return changed;
2445 }
2446
2447 for (const bUnitDef *unit = usys->units; unit->name; unit++) {
2448 /* In case there are multiple instances. */
2449 while (unit_replace(str, str_maxncpy, str_tmp, scale_pref_base, unit)) {
2450 changed = true;
2451 }
2452 }
2453
2454 /* Try other unit systems now, so we can evaluate imperial when metric is set for eg. */
2455 /* Note that checking other systems at that point means we do not support their units as
2456 * 'default' one. In other words, when in metrics, typing '2+2in' will give 2 meters 2 inches,
2457 * not 4 inches. I do think this is the desired behavior!
2458 */
2459 for (int system_iter = 0; system_iter < UNIT_SYSTEM_TOT; system_iter++) {
2460 if (system_iter == system) {
2461 continue;
2462 }
2463 const bUnitCollection *usys_iter = unit_get_system(system_iter, type);
2464 if (usys_iter == nullptr) {
2465 continue;
2466 }
2467
2468 for (const bUnitDef *unit = usys_iter->units; unit->name; unit++) {
2469 int ofs = 0;
2470 /* In case there are multiple instances. */
2471 while ((ofs = unit_replace(str + ofs, str_maxncpy - ofs, str_tmp, scale_pref_base, unit))) {
2472 changed = true;
2473 }
2474 }
2475 }
2476
2477 /* Replace # with add sign when there is no operator between it and the next number.
2478 *
2479 * "1*1# 3*100# * 3" -> "1*1+ 3*100 * 3"
2480 */
2481 {
2482 char *str_found = str;
2483
2484 while ((str_found = strchr(str_found, SEP_CHR))) {
2485 bool op_found = false;
2486
2487 /* Any operators after this? */
2488 for (const char *ch = str_found + 1; *ch != '\0'; ch++) {
2489 if (ELEM(*ch, ' ', '\t')) {
2490 continue;
2491 }
2492 op_found = (ch_is_op(*ch) || ELEM(*ch, ',', ')'));
2493 break;
2494 }
2495
2496 /* If found an op, comma or closing parenthesis, no need to insert a '+', else we need it. */
2497 *str_found++ = op_found ? ' ' : '+';
2498 }
2499 }
2500
2501 return changed;
2502}
2503
2504void BKE_unit_name_to_alt(char *str, int str_maxncpy, const char *orig_str, int system, int type)
2505{
2506 const bUnitCollection *usys = unit_get_system(system, type);
2507
2508 /* Find and substitute all units. */
2509 for (const bUnitDef *unit = usys->units; unit->name && (str_maxncpy > 0); unit++) {
2510 if (unit->name_alt == nullptr) {
2511 continue;
2512 }
2513 const bool case_sensitive = (unit->flag & B_UNIT_DEF_CASE_SENSITIVE) != 0;
2514 const char *found = unit_find_str(orig_str, unit->name_short, case_sensitive);
2515 if (found == nullptr) {
2516 continue;
2517 }
2518
2519 int offset = int(found - orig_str);
2520
2521 /* Copy everything before the unit. */
2522 if (offset < str_maxncpy) {
2523 memcpy(str, orig_str, offset);
2524 }
2525 else {
2526 BLI_strncpy(str, orig_str, str_maxncpy);
2527 offset = str_maxncpy;
2528 }
2529
2530 str += offset;
2531 orig_str += offset + strlen(unit->name_short);
2532 str_maxncpy -= offset;
2533
2534 /* Print the alt_name. */
2535 const int len_name = BLI_strncpy_rlen(str, unit->name_alt, str_maxncpy);
2536 BLI_assert(len_name < str_maxncpy);
2537 str += len_name;
2538 str_maxncpy -= len_name;
2539 }
2540
2541 /* Finally copy the rest of the string. */
2542 BLI_strncpy(str, orig_str, str_maxncpy);
2543}
2544
2545double BKE_unit_closest_scalar(double value, int system, int type)
2546{
2547 const bUnitCollection *usys = unit_get_system(system, type);
2548
2549 if (usys == nullptr) {
2550 return -1;
2551 }
2552
2553 const bUnitDef *unit = unit_best_fit(value, usys, nullptr, 1);
2554 if (unit == nullptr) {
2555 return -1;
2556 }
2557
2558 return unit->scalar;
2559}
2560
2561double BKE_unit_base_scalar(int system, int type)
2562{
2563 const bUnitCollection *usys = unit_get_system(system, type);
2564 if (usys) {
2565 return unit_default(usys)->scalar;
2566 }
2567
2568 return 1.0;
2569}
2570
2571bool BKE_unit_is_valid(int system, int type)
2572{
2573 return !(system < 0 || system > UNIT_SYSTEM_TOT || type < 0 || type > B_UNIT_TYPE_TOT);
2574}
2575
2576void BKE_unit_system_get(int system, int type, void const **r_usys_pt, int *r_len)
2577{
2578 const bUnitCollection *usys = unit_get_system(system, type);
2579 *r_usys_pt = usys;
2580
2581 if (usys == nullptr) {
2582 *r_len = 0;
2583 return;
2584 }
2585
2586 *r_len = usys->length;
2587}
2588
2589int BKE_unit_base_get(const void *usys_pt)
2590{
2591 return (static_cast<bUnitCollection *>(const_cast<void *>(usys_pt)))->base_unit;
2592}
2593
2594int BKE_unit_base_of_type_get(int system, int type)
2595{
2596 return unit_get_system(system, type)->base_unit;
2597}
2598
2600{
2602 const int unit_index = get_preferred_display_unit_index_if_used(type, units);
2603 if (unit_index == -1) {
2604 return BKE_unit_base_of_type_get(units.system, type);
2605 }
2606 return unit_index;
2607}
2608
2609const char *BKE_unit_name_get(const void *usys_pt, int index)
2610{
2611 const bUnitCollection *usys = static_cast<const bUnitCollection *>(usys_pt);
2612 BLI_assert(uint(index) < uint(usys->length));
2613 return usys->units[index].name;
2614}
2615const char *BKE_unit_display_name_get(const void *usys_pt, int index)
2616{
2617 const bUnitCollection *usys = static_cast<const bUnitCollection *>(usys_pt);
2618 BLI_assert(uint(index) < uint(usys->length));
2619 return usys->units[index].name_display;
2620}
2621const char *BKE_unit_display_name_short_get(const void *usys_pt, int index)
2622{
2623 const bUnitCollection *usys = static_cast<const bUnitCollection *>(usys_pt);
2624 BLI_assert(uint(index) < uint(usys->length));
2625 return usys->units[index].name_short;
2626}
2627const char *BKE_unit_identifier_get(const void *usys_pt, int index)
2628{
2629 const bUnitCollection *usys = static_cast<const bUnitCollection *>(usys_pt);
2630 BLI_assert(uint(index) < uint(usys->length));
2631 const bUnitDef *unit = &usys->units[index];
2632 if (unit->identifier == nullptr) {
2633 BLI_assert_msg(0, "identifier for this unit is not specified yet");
2634 }
2635 return unit->identifier;
2636}
2637
2638double BKE_unit_scalar_get(const void *usys_pt, int index)
2639{
2640 const bUnitCollection *usys = static_cast<const bUnitCollection *>(usys_pt);
2641 BLI_assert(uint(index) < uint(usys->length));
2642 return usys->units[index].scalar;
2643}
2644
2645bool BKE_unit_is_suppressed(const void *usys_pt, int index)
2646{
2647 const bUnitCollection *usys = static_cast<const bUnitCollection *>(usys_pt);
2648 BLI_assert(uint(index) < uint(usys->length));
2649 return (usys->units[index].flag & B_UNIT_DEF_SUPPRESS) != 0;
2650}
2651
2652bool BKE_unit_is_adaptive(const UnitSettings &settings, int type)
2653{
2655 return is_unit_adaptive(type, units);
2656}
2657
2659{
2660 const PreferredUnits units = preferred_units_from_UnitSettings(settings);
2661 const bUnitCollection *usys = unit_get_system(units.system, unit_type);
2662
2663 if (!is_valid_unit_collection(usys)) {
2664 return {};
2665 }
2666
2667 const bUnitDef *unit = get_preferred_display_unit_if_used(unit_type, units);
2668 if (unit == nullptr) {
2669 /* The preferred unit is adaptive, which picks a unit per value at display time. */
2670 unit = unit_default(usys);
2671 }
2672 return {unit->scalar, unit->bias};
2673}
2674
2675} // namespace blender
#define M_PI
#define SNPRINTF(dst, format,...)
#define BLI_STR_UTF8_DEGREE_SIGN
#define BLI_STR_UTF8_SUPERSCRIPT_2
#define BLI_STR_UTF8_SUPERSCRIPT_3
Compatibility-like things for windows.
#define USER_UNIT_ADAPTIVE
int pad[32 - sizeof(int)]
#define CLAMP(a, b, c)
#define BLI_INLINE
#define ELEM(...)
#define BLI_assert_msg(a, msg)
#define str(s)
#define pow
float length(VecOp< float, D >) RET
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
void split(const std::string &s, const char delim, std::vector< std::string > &tokens)
Definition abc_util.cc:68
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static bUnitDef buImperialVelDef[]
Definition unit.cc:1016
static const bUnitCollection * unit_get_system(int system, int type)
Definition unit.cc:1583
double BKE_unit_scalar_get(const void *usys_pt, int index)
Definition unit.cc:2638
static bUnitDef buPowerDef[]
Definition unit.cc:1309
static const bUnitDef * unit_best_fit(double value, const bUnitCollection *usys, const bUnitDef *unit_start, int suppress)
Definition unit.cc:1595
static bUnitDef buMetricLenDef[]
Definition unit.cc:177
double BKE_unit_value_scale(const UnitSettings &settings, int unit_type, double value)
Definition unit.cc:1963
const char * BKE_unit_identifier_get(const void *usys_pt, int index)
Definition unit.cc:2627
static bUnitDef buFrequencyDef[]
Definition unit.cc:1488
static bool unit_find(const char *str, const bUnitDef *unit)
Definition unit.cc:2334
@ B_UNIT_DEF_NO_SPACE
Definition unit.cc:122
@ B_UNIT_DEF_NONE
Definition unit.cc:114
@ B_UNIT_DEF_SUPPRESS
Definition unit.cc:116
@ B_UNIT_DEF_CASE_SENSITIVE
Definition unit.cc:120
@ B_UNIT_DEF_TENTH
Definition unit.cc:118
unsigned int uint
Definition BKE_text.h:55
double BKE_unit_base_scalar(int system, int type)
Definition unit.cc:2561
static bUnitDef buImperialVolDef[]
Definition unit.cc:736
static bUnitDef buMetricVelDef[]
Definition unit.cc:983
static bool is_unit_adaptive(const int type, const PreferredUnits &units)
Definition unit.cc:1795
static bUnitDef buImperialTempDef[]
Definition unit.cc:1431
static int get_preferred_display_unit_index_if_used(int type, const PreferredUnits &units)
Definition unit.cc:1814
static bUnitCollection buMetricTempCollection
Definition unit.cc:1424
static const bUnitCollection buMetricLenCollection
Definition unit.cc:296
static bUnitDef buImperialAclDef[]
Definition unit.cc:1072
static bUnitCollection buWavelengthLenCollection
Definition unit.cc:441
static bool ch_is_op_unary(char op)
Definition unit.cc:2069
bool BKE_unit_is_suppressed(const void *usys_pt, int index)
Definition unit.cc:2645
static bUnitDef buMetricVolDef[]
Definition unit.cc:637
static bUnitDef buMetricMassDef[]
Definition unit.cc:825
static size_t unit_as_string(char *str, int str_maxncpy, double value, int prec, const bool variable_width, const bUnitCollection *usys, const bUnitDef *unit, char pad, bool do_unit_suffix)
Definition unit.cc:1643
static bUnitCollection buImperialVelCollection
Definition unit.cc:1042
UnitConversion BKE_unit_preferred_conversion_get(const UnitSettings &settings, int unit_type)
Definition unit.cc:2658
MINLINE int max_ii(int a, int b)
static bUnitDef buImperialMassDef[]
Definition unit.cc:913
int BKE_unit_base_of_type_get(int system, int type)
Definition unit.cc:2594
int char * BLI_strcasestr(const char *s, const char *find) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
size_t BLI_snprintf_rlen(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
static bUnitDef buImperialAreaDef[]
Definition unit.cc:548
static int find_previous_non_value_char(const char *str, const int start_ofs)
Definition unit.cc:2205
const char * BKE_unit_display_name_short_get(const void *usys_pt, int index)
Definition unit.cc:2621
static bUnitCollection buImperialAreaCollection
Definition unit.cc:629
bool BKE_unit_replace_string(char *str, int str_maxncpy, const char *str_prev, double scale_pref, int system, int type)
Definition unit.cc:2414
static int unit_replace(char *str, int str_maxncpy, char *str_tmp, double scale_pref, const bUnitDef *unit)
Definition unit.cc:2318
static bUnitCollection buNaturalRotCollection
Definition unit.cc:1234
static bUnitCollection buNaturalTimeCollection
Definition unit.cc:1166
static const char * unit_find_str(const char *str, const char *substr, bool case_sensitive)
Definition unit.cc:1994
static char * find_next_op(const char *str, char *remaining_str, int remaining_str_maxncpy)
Definition unit.cc:2113
static const bUnitDef * get_preferred_display_unit_if_used(int type, const PreferredUnits &units)
Definition unit.cc:1851
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
static bUnitCollection buImperialAclCollection
Definition unit.cc:1087
static const bUnitCollection * bUnitSystems[][B_UNIT_TYPE_TOT]
Definition unit.cc:1522
static bUnitCollection buMetricVelCollection
Definition unit.cc:1009
static bUnitCollection buFrequencyCollection
Definition unit.cc:1514
int BKE_preffered_unit_of_type_or_base_get(const UnitSettings &settings, int type)
Definition unit.cc:2599
static bUnitDef buColorTempDef[]
Definition unit.cc:1465
static bUnitCollection buDummyCollection
Definition unit.cc:169
static bool unit_should_be_split(int type)
Definition unit.cc:1724
static bUnitCollection buMetricVolCollection
Definition unit.cc:729
static bUnitCollection buMetricAclCollection
Definition unit.cc:1065
static bool is_valid_unit_collection(const bUnitCollection *usys)
Definition unit.cc:1790
static size_t unit_as_string_split_pair(char *str, int str_maxncpy, double value, int prec, const bool variable_width, const bUnitCollection *usys, const bUnitDef *main_unit)
Definition unit.cc:1751
static bool ch_is_op(char op)
Definition unit.cc:2047
char char size_t BLI_strncpy_rlen(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL(1
static int find_end_of_value_chars(const char *str, const int str_maxncpy, const int start_ofs)
Definition unit.cc:2219
static bool unit_distribute_negatives(char *str, const int str_maxncpy)
Definition unit.cc:2167
size_t BKE_unit_value_as_string_adaptive(char *str, int str_maxncpy, double value, int prec, int system, int type, bool split, bool pad, bool do_unit_suffix)
Definition unit.cc:1908
void BKE_unit_system_get(int system, int type, const void **r_usys_pt, int *r_len)
Definition unit.cc:2576
size_t BKE_unit_value_as_string(char *str, int str_maxncpy, double value, int prec, int type, const UnitSettings &settings, bool pad, bool do_unit_suffix)
Definition unit.cc:1929
static const bUnitDef * unit_detect_from_str(const bUnitCollection *usys, const char *str, const char *str_prev)
Definition unit.cc:2369
bool BKE_unit_string_contains_unit(const char *str, int type)
Definition unit.cc:2388
bool BKE_unit_is_valid(int system, int type)
Definition unit.cc:2571
size_t BKE_unit_value_as_string_scaled(char *str, int str_maxncpy, double value, int prec, int type, const UnitSettings &settings, bool pad, bool do_unit_suffix)
Definition unit.cc:1944
static bUnitDef buMetricAclDef[]
Definition unit.cc:1050
double BKE_unit_closest_scalar(double value, int system, int type)
Definition unit.cc:2545
static bUnitCollection buMetricMassCollection
Definition unit.cc:906
static PreferredUnits preferred_units_from_UnitSettings(const UnitSettings &settings)
Definition unit.cc:1739
static void unit_dual_convert(double value, const bUnitCollection *usys, bUnitDef const **r_unit_a, bUnitDef const **r_unit_b, double *r_value_a, double *r_value_b, const bUnitDef *main_unit)
Definition unit.cc:1625
const char * BKE_unit_name_get(const void *usys_pt, int index)
Definition unit.cc:2609
const char * BLI_str_find_prev_char_utf8(const char *p, const char *str_start) ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL ATTR_NONNULL(1
static const bUnitDef * unit_default(const bUnitCollection *usys)
Definition unit.cc:1590
static bUnitDef buMetricTempDef[]
Definition unit.cc:1398
int BKE_unit_base_get(const void *usys_pt)
Definition unit.cc:2589
static bUnitDef buNaturalTimeDef[]
Definition unit.cc:1095
static int unit_scale_str(char *str, int str_maxncpy, char *str_tmp, double scale_pref, const bUnitDef *unit, const char *replace_str, bool case_sensitive)
Definition unit.cc:2230
static bUnitDef buNaturalRotDef[]
Definition unit.cc:1173
static bUnitCollection buImperialTempCollection
Definition unit.cc:1457
static bUnitCollection buImperialVolCollection
Definition unit.cc:817
void BKE_unit_name_to_alt(char *str, int str_maxncpy, const char *orig_str, int system, int type)
Definition unit.cc:2504
static bUnitDef buMetricAreaDef[]
Definition unit.cc:449
static bUnitDef buImperialLenDef[]
Definition unit.cc:303
MINLINE int integer_digits_d(double d)
const char * BKE_unit_display_name_get(const void *usys_pt, int index)
Definition unit.cc:2615
static bUnitCollection buCameraLenCollection
Definition unit.cc:1301
static char * skip_unary_op(char *str)
Definition unit.cc:2153
@ B_UNIT_CAMERA
Definition BKE_unit.hh:175
@ B_UNIT_MASS
Definition BKE_unit.hh:169
@ B_UNIT_AREA
Definition BKE_unit.hh:167
@ B_UNIT_LENGTH
Definition BKE_unit.hh:166
@ B_UNIT_TYPE_TOT
Definition BKE_unit.hh:181
@ B_UNIT_ACCELERATION
Definition BKE_unit.hh:174
@ B_UNIT_TIME
Definition BKE_unit.hh:171
@ B_UNIT_VELOCITY
Definition BKE_unit.hh:173
@ B_UNIT_VOLUME
Definition BKE_unit.hh:168
@ B_UNIT_POWER
Definition BKE_unit.hh:176
@ B_UNIT_ROTATION
Definition BKE_unit.hh:170
@ B_UNIT_TEMPERATURE
Definition BKE_unit.hh:177
@ B_UNIT_WAVELENGTH
Definition BKE_unit.hh:178
static bUnitCollection buImperialMassCollection
Definition unit.cc:972
BLI_INLINE bool isalpha_or_utf8(const int ch)
Definition unit.cc:1989
static const bUnitDef * unit_find_in_collection(const bUnitCollection *usys, const char *str)
Definition unit.cc:2353
static bUnitDef buWavelengthLenDef[]
Definition unit.cc:393
bool BKE_unit_is_adaptive(const UnitSettings &settings, int type)
Definition unit.cc:2652
static bUnitCollection buImperialLenCollection
Definition unit.cc:385
static bUnitDef buCameraLenDef[]
Definition unit.cc:1242
static const char * find_next_negative(const char *str, const char *remaining_str)
Definition unit.cc:2087
static bUnitCollection buPowerCollection
Definition unit.cc:1390
static bUnitCollection buMetricAreaCollection
Definition unit.cc:541
double BKE_unit_apply_preferred_unit(const UnitSettings &settings, int type, double value)
Definition unit.cc:2402
static bUnitDef buDummyDef[]
Definition unit.cc:154
static size_t unit_as_string_main(char *str, int str_maxncpy, double value, int prec, int type, bool split, bool pad, bool do_unit_suffix, const PreferredUnits &units)
Definition unit.cc:1862
static bUnitCollection buColorTempCollection
Definition unit.cc:1480
const bUnitDef * units
Definition unit.cc:127
const char * name_display
Definition unit.cc:103
const char * name_short
Definition unit.cc:95
const char * name
Definition unit.cc:91
const char * name_plural
Definition unit.cc:93
const char * identifier
Definition unit.cc:105
const char * name_alt
Definition unit.cc:100
double scalar
Definition unit.cc:107
#define UN_SC_ITON
Definition unit.cc:75
#define UN_SC_FUR
Definition unit.cc:58
#define UN_SC_FT
Definition unit.cc:61
#define UN_SC_DAM
Definition unit.cc:49
#define UN_SC_CWT
Definition unit.cc:76
#define UN_SC_DM
Definition unit.cc:51
#define UN_SC_MG
Definition unit.cc:72
#define UN_SC_MTON
Definition unit.cc:66
#define UN_SC_HG
Definition unit.cc:69
#define SEP_CHR
Definition unit.cc:40
#define UN_SC_KM
Definition unit.cc:47
#define UN_SC_G
Definition unit.cc:71
#define UN_SC_IN
Definition unit.cc:62
#define NULL_UNIT
Definition unit.cc:140
#define UNIT_COLLECTION_LENGTH(def)
Definition unit.cc:136
#define UN_SC_LB
Definition unit.cc:78
#define UN_SC_MIL
Definition unit.cc:63
#define TEMP_STR_SIZE
Definition unit.cc:37
#define EPS
Definition unit.cc:44
#define UN_SC_MI
Definition unit.cc:57
#define UN_SC_CH
Definition unit.cc:59
#define UN_SC_YD
Definition unit.cc:60
#define UN_SC_ST
Definition unit.cc:77
#define UN_SC_QL
Definition unit.cc:67
#define UN_SC_OZ
Definition unit.cc:79
#define UN_SC_DAG
Definition unit.cc:70
#define UN_SC_MM
Definition unit.cc:53
#define UN_SC_CM
Definition unit.cc:52
#define UN_SC_FAH
Definition unit.cc:82
#define UN_SC_UM
Definition unit.cc:54
#define UN_SC_KG
Definition unit.cc:68
#define UN_SC_HM
Definition unit.cc:48
#define SEP_STR
Definition unit.cc:41
#define UN_SC_M
Definition unit.cc:50
#define UNIT_SYSTEM_TOT
Definition unit.cc:1521