Root/
| 1 | /* |
| 2 | * coord.h - Coordinate representation and basic operations |
| 3 | * |
| 4 | * Written 2009, 2010, 2016 by Werner Almesberger |
| 5 | * Copyright 2009, 2010, 2016 by Werner Almesberger |
| 6 | * Copyright 2016, Erich Heinzle (gEDA additions) |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | */ |
| 13 | |
| 14 | |
| 15 | #ifndef COORD_H |
| 16 | #define COORD_H |
| 17 | |
| 18 | #include <stdint.h> |
| 19 | |
| 20 | |
| 21 | #define MICRON_UNITS 10 |
| 22 | #define MIL_UNITS (25.4*MICRON_UNITS) |
| 23 | #define MM_UNITS (1000.0*MICRON_UNITS) |
| 24 | #define UM_UNITS MICRON_UNITS |
| 25 | #define KICAD_UNIT (MIL_UNITS/10.0) |
| 26 | #define GEDA_UNIT (MIL_UNITS/100.0) |
| 27 | |
| 28 | #define MIL_IN_MM 0.0254 |
| 29 | #define UM_IN_MM 0.001 |
| 30 | |
| 31 | |
| 32 | typedef int32_t unit_type; |
| 33 | |
| 34 | |
| 35 | #define UNIT_ERROR ((unit_type) 1 << (sizeof(unit_type)*8-1)) |
| 36 | |
| 37 | |
| 38 | struct coord { |
| 39 | unit_type x, y; |
| 40 | }; |
| 41 | |
| 42 | |
| 43 | static inline unit_type mil_to_units(double mil) |
| 44 | { |
| 45 | return mil*MIL_UNITS; |
| 46 | } |
| 47 | |
| 48 | |
| 49 | static inline unit_type mm_to_units(double mm) |
| 50 | { |
| 51 | return mm*MM_UNITS; |
| 52 | } |
| 53 | |
| 54 | |
| 55 | static inline unit_type um_to_units(double mm) |
| 56 | { |
| 57 | return mm*UM_UNITS; |
| 58 | } |
| 59 | |
| 60 | |
| 61 | static inline double units_to_mm(unit_type u) |
| 62 | { |
| 63 | return (double) u/MM_UNITS; |
| 64 | } |
| 65 | |
| 66 | |
| 67 | static inline double units_to_um(unit_type u) |
| 68 | { |
| 69 | return (double) u/UM_UNITS; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | static inline double units_to_mil(unit_type u) |
| 74 | { |
| 75 | return (double) u/MIL_UNITS; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | static inline int units_to_kicad(unit_type u) |
| 80 | { |
| 81 | return (double) u/KICAD_UNIT; |
| 82 | } |
| 83 | |
| 84 | static inline int units_to_geda(unit_type u) |
| 85 | { |
| 86 | return (double) u/GEDA_UNIT; |
| 87 | } |
| 88 | |
| 89 | static inline int coord_eq(struct coord a, struct coord b) |
| 90 | { |
| 91 | return a.x == b.x && a.y == b.y; |
| 92 | } |
| 93 | |
| 94 | |
| 95 | double mm_to_mil(double mm, int exponent); |
| 96 | double mil_to_mm(double mil, int exponent); |
| 97 | double um_to_mm(double um, int exponent); |
| 98 | |
| 99 | double units_to_best(unit_type u, int *mm); |
| 100 | |
| 101 | struct coord normalize(struct coord v, unit_type len); |
| 102 | struct coord rotate(struct coord v, double angle); |
| 103 | struct coord add_vec(struct coord a, struct coord b); |
| 104 | struct coord sub_vec(struct coord a, struct coord b); |
| 105 | struct coord neg_vec(struct coord v); |
| 106 | |
| 107 | struct coord rotate_r(struct coord c, unit_type r, double angle); |
| 108 | double theta_vec(struct coord v); |
| 109 | double theta(struct coord c, struct coord p); |
| 110 | |
| 111 | void sort_coord(struct coord *min, struct coord *max); |
| 112 | |
| 113 | unit_type dist_point(struct coord a, struct coord b); |
| 114 | unit_type dist_line(struct coord p, struct coord a, struct coord b); |
| 115 | unit_type dist_rect(struct coord p, struct coord a, struct coord b); |
| 116 | int inside_rect(struct coord p, struct coord a, struct coord b); |
| 117 | unit_type dist_circle(struct coord p, struct coord c, unit_type r); |
| 118 | |
| 119 | #endif /* !COORD_H */ |
| 120 |
Branches:
master
