summaryrefslogtreecommitdiff
path: root/inc/cad/solve.h
blob: a470aae3bc3ea54c1ee2b57e8af569d05da1f8e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#pragma once

#include "cad/construction.h"

#define SEARCH_DEPTH 16

enum component_type {
	COM_POINT,
	COM_LINE,
};

struct component {
	enum component_type type;

	struct element *e;

	bool show_when_placed;

	double min;
	double max;
	bool min_max_init;
	bool drawn;
};

struct alias {
	struct component *alias;
	struct component *target;
};

struct constraints {
	struct constraint *elements;
	size_t length;
	size_t capacity;

	struct alias aliases[16];
	size_t aliases_num;
};

extern char *constraint_type_name[];
enum constraint_type {
	CT_POINT_POINT_DISTANCE,
	CT_POINT_LINE_DISTANCE,
	CT_LINE_LINE_ANGLE,

	CT_END,
};

#define PP_DISTANCE(C1, C2, D) \
	{ \
		.type = CT_POINT_POINT_DISTANCE, \
		.v = D, \
		.c1 = C1, \
		.c2 = C2, \
	}

#define PP_ALIAS(C1, C2) \
	PP_DISTANCE(C1, C2, 0)

#define PP_SAME(C1, C2) \
	PP_DISTANCE(C1, C2, 0)

#define PL_DISTANCE(C1, C2, D) \
	{ \
		.type = CT_POINT_LINE_DISTANCE, \
		.v = D, \
		.c1 = C1, \
		.c2 = C2, \
	}

#define POINT_ON_LINE(C1, C2) \
	PL_DISTANCE(C1, C2, 0)

#define LL_ANGLE(C1, C2, D) \
	{ \
		.type = CT_LINE_LINE_ANGLE, \
		.v = D, \
		.c1 = C1, \
		.c2 = C2, \
	}

#define CEND() \
	{ \
		.type = CT_END, \
	}

struct path_step {
	uint64_t i;
	bool direction;
};

struct constraint {
	enum constraint_type type;
	double v;

	struct component *c1;
	struct component *c2;

	uint64_t order;
	struct path_step path[SEARCH_DEPTH];
	bool forward;
	bool used;
};

void alias_point(struct constraints *c, struct component *alias, struct component *target);
void add_constraint(struct constraints *c, struct constraint *new);
void free_constraints(struct constraints *c);
bool solve_constraints(struct constraints *constraints, struct drawing *drawing);

#define DEG(x) ((x) * M_PI / 180.0)