2009-08-03 19:12:47 +03:00
|
|
|
/*
|
|
|
|
* coord.h - Coordinate representation and basic operations
|
|
|
|
*
|
2016-12-31 22:46:39 +02:00
|
|
|
* Written 2009, 2010, 2016 by Werner Almesberger
|
|
|
|
* Copyright 2009, 2010, 2016 by Werner Almesberger
|
2009-08-03 19:12:47 +03:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef COORD_H
|
|
|
|
#define COORD_H
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
|
|
|
|
#define MICRON_UNITS 10
|
|
|
|
#define MIL_UNITS (25.4*MICRON_UNITS)
|
|
|
|
#define MM_UNITS (1000.0*MICRON_UNITS)
|
2016-12-31 22:46:39 +02:00
|
|
|
#define UM_UNITS MICRON_UNITS
|
2009-08-11 00:02:26 +03:00
|
|
|
#define KICAD_UNIT (MIL_UNITS/10.0)
|
2009-08-03 19:12:47 +03:00
|
|
|
|
|
|
|
#define MIL_IN_MM 0.0254
|
2016-12-31 22:46:39 +02:00
|
|
|
#define UM_IN_MM 0.001
|
2009-08-03 19:12:47 +03:00
|
|
|
|
|
|
|
|
|
|
|
typedef int32_t unit_type;
|
|
|
|
|
|
|
|
|
|
|
|
#define UNIT_ERROR ((unit_type) 1 << (sizeof(unit_type)*8-1))
|
|
|
|
|
|
|
|
|
|
|
|
struct coord {
|
|
|
|
unit_type x, y;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
static inline unit_type mil_to_units(double mil)
|
|
|
|
{
|
|
|
|
return mil*MIL_UNITS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static inline unit_type mm_to_units(double mm)
|
|
|
|
{
|
|
|
|
return mm*MM_UNITS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-31 22:46:39 +02:00
|
|
|
static inline unit_type um_to_units(double mm)
|
|
|
|
{
|
|
|
|
return mm*UM_UNITS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-03 19:12:47 +03:00
|
|
|
static inline double units_to_mm(unit_type u)
|
|
|
|
{
|
|
|
|
return (double) u/MM_UNITS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-12-31 22:46:39 +02:00
|
|
|
static inline double units_to_um(unit_type u)
|
|
|
|
{
|
|
|
|
return (double) u/UM_UNITS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-15 23:52:08 +03:00
|
|
|
static inline double units_to_mil(unit_type u)
|
|
|
|
{
|
|
|
|
return (double) u/MIL_UNITS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-11 00:02:26 +03:00
|
|
|
static inline int units_to_kicad(unit_type u)
|
2009-08-03 19:12:47 +03:00
|
|
|
{
|
|
|
|
return (double) u/KICAD_UNIT;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-08 21:50:17 +03:00
|
|
|
static inline int coord_eq(struct coord a, struct coord b)
|
|
|
|
{
|
|
|
|
return a.x == b.x && a.y == b.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-08-03 19:12:47 +03:00
|
|
|
double mm_to_mil(double mm, int exponent);
|
|
|
|
double mil_to_mm(double mil, int exponent);
|
2016-12-31 22:46:39 +02:00
|
|
|
double um_to_mm(double um, int exponent);
|
2009-08-03 19:12:47 +03:00
|
|
|
|
2009-08-15 23:52:08 +03:00
|
|
|
double units_to_best(unit_type u, int *mm);
|
|
|
|
|
2009-08-03 19:12:47 +03:00
|
|
|
struct coord normalize(struct coord v, unit_type len);
|
|
|
|
struct coord rotate(struct coord v, double angle);
|
|
|
|
struct coord add_vec(struct coord a, struct coord b);
|
|
|
|
struct coord sub_vec(struct coord a, struct coord b);
|
|
|
|
struct coord neg_vec(struct coord v);
|
|
|
|
|
2009-08-05 00:45:33 +03:00
|
|
|
struct coord rotate_r(struct coord c, unit_type r, double angle);
|
2009-12-15 21:33:15 +02:00
|
|
|
double theta_vec(struct coord v);
|
2009-08-05 00:45:33 +03:00
|
|
|
double theta(struct coord c, struct coord p);
|
|
|
|
|
|
|
|
void sort_coord(struct coord *min, struct coord *max);
|
|
|
|
|
2009-08-03 19:12:47 +03:00
|
|
|
unit_type dist_point(struct coord a, struct coord b);
|
|
|
|
unit_type dist_line(struct coord p, struct coord a, struct coord b);
|
2009-08-05 00:45:33 +03:00
|
|
|
unit_type dist_rect(struct coord p, struct coord a, struct coord b);
|
|
|
|
int inside_rect(struct coord p, struct coord a, struct coord b);
|
2009-08-03 19:12:47 +03:00
|
|
|
unit_type dist_circle(struct coord p, struct coord c, unit_type r);
|
|
|
|
|
|
|
|
#endif /* !COORD_H */
|