aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Embroidery/libembroidery/emb-line.c
blob: ad5312d36a260b1f4bb1e1e7631fe4f6a82a12ea (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "emb-line.h"
#include "emb-logging.h"
#include "emb-vector.h"
#include <stdlib.h>

/**************************************************/
/* EmbLine                                        */
/**************************************************/

double embLine_x1(EmbLine line)
{
    return line.x1;
}

double embLine_y1(EmbLine line)
{
    return line.y1;
}

double embLine_x2(EmbLine line)
{
    return line.x2;
}

double embLine_y2(EmbLine line)
{
    return line.y2;
}

/**************************************************/
/* EmbLineObject                                  */
/**************************************************/

/* Returns an EmbLineObject. It is created on the stack. */
EmbLineObject embLineObject_make(double x1, double y1, double x2, double y2)
{
    EmbLineObject stackLineObj;
    stackLineObj.line.x1 = x1;
    stackLineObj.line.y1 = y1;
    stackLineObj.line.x2 = x2;
    stackLineObj.line.y2 = y2;
    return stackLineObj;
}

/* Returns a pointer to an EmbLineObject. It is created on the heap. The caller is responsible for freeing the allocated memory. */
EmbLineObject* embLineObject_create(double x1, double y1, double x2, double y2)
{
    EmbLineObject* heapLineObj = (EmbLineObject*)malloc(sizeof(EmbLineObject));
    if(!heapLineObj) { embLog_error("emb-line.c embLineObject_create(), cannot allocate memory for heapLineObj\n"); return 0; }
    heapLineObj->line.x1 = x1;
    heapLineObj->line.y1 = y1;
    heapLineObj->line.x2 = x2;
    heapLineObj->line.y2 = y2;
    return heapLineObj;
}

/**************************************************/
/* EmbLineObjectList                              */
/**************************************************/

EmbLineObjectList* embLineObjectList_create(EmbLineObject data)
{
    EmbLineObjectList* heapLineObjList = (EmbLineObjectList*)malloc(sizeof(EmbLineObjectList));
    if(!heapLineObjList) { embLog_error("emb-line.c embLineObjectList_create(), cannot allocate memory for heapLineObjList\n"); return 0; }
    heapLineObjList->lineObj = data;
    heapLineObjList->next = 0;
    return heapLineObjList;
}

EmbLineObjectList* embLineObjectList_add(EmbLineObjectList* pointer, EmbLineObject data)
{
    if(!pointer) { embLog_error("emb-line.c embLineObjectList_add(), pointer argument is null\n"); return 0; }
    if(pointer->next) { embLog_error("emb-line.c embLineObjectList_add(), pointer->next should be null\n"); return 0; }
    pointer->next = (EmbLineObjectList*)malloc(sizeof(EmbLineObjectList));
    if(!pointer->next) { embLog_error("emb-line.c embLineObjectList_add(), cannot allocate memory for pointer->next\n"); return 0; }
    pointer = pointer->next;
    pointer->lineObj = data;
    pointer->next = 0;
    return pointer;
}

int embLineObjectList_count(EmbLineObjectList* pointer)
{
    int i = 1;
    if(!pointer) return 0;
    while(pointer->next)
    {
        pointer = pointer->next;
        i++;
    }
    return i;
}

int embLineObjectList_empty(EmbLineObjectList* pointer)
{
    if(!pointer)
        return 1;
    return 0;
}

void embLineObjectList_free(EmbLineObjectList* pointer)
{
    EmbLineObjectList* tempPointer = pointer;
    EmbLineObjectList* nextPointer = 0;
    while(tempPointer)
    {
        nextPointer = tempPointer->next;
        free(tempPointer);
        tempPointer = nextPointer;
    }
    pointer = 0;
}

/* TODO: API Cleanup: This function should use an embLine parameter, not vector1/vector2, and look like this:
                      embLine_normalVector(EmbLine line, EmbVector* result, int clockwise) */
/*! Finds the normalized vector perpendicular (clockwise) to the line given by v1->v2 (normal to the line) */
void embLine_normalVector(EmbVector vector1, EmbVector vector2, EmbVector* result, int clockwise)
{
    double temp;
    if(!result) { embLog_error("emb-line.c embLine_normalVector(), result argument is null\n"); return; }
    result->X = vector2.X - vector1.X;
    result->Y = vector2.Y - vector1.Y;
    embVector_normalize(*result, result);
    temp = result->X;
    result->X = result->Y;
    result->Y = -temp;
    if(!clockwise)
    {
        result->X = -result->X;
        result->Y = -result->Y;
    }
}

/* TODO: API Cleanup: This is similar to the code we already have in geom-line.c, but may work well here.
         Also the result has NOTHING to do with vectors. It sets a point, not a vector, even though
         vector math is used internally inside this function. The function should look like this:
         embLine_intersectionPoint(EmbLine line1, EmbLine line2, EmbPoint* result)
         */
/*! Finds the intersection of two lines given by v1->v2 and v3->v4 and sets the value in the result variable */
void embLine_intersectionPoint(EmbVector v1, EmbVector v2, EmbVector v3, EmbVector v4, EmbVector* result)
{
    double A2 = v2.Y - v1.Y;
    double B2 = v1.X - v2.X;
    double C2 = A2 * v1.X + B2 * v1.Y;

    double A1 = v4.Y - v3.Y;
    double B1 = v3.X - v4.X;
    double C1 = A1 * v3.X + B1 * v3.Y;

    double det = A1 * B2 - A2 * B1;

    if(!result) { embLog_error("emb-line.c embLine_intersectionPoint(), result argument is null\n"); return; }
    /*TODO: The code below needs revised since division by zero can still occur */
    if(det < 1e-10 && det > -1e-10)
    {
        result->X = -10000; /* TODO: What is significant about these numbers? Leave the point, undefined */
        result->Y = -10000; /* TODO: A better solution would be to return an unsigned char(0 parallel, 1 intersecting, 2 not-intersecting) */
    }
    result->X = (B2 * C1 - B1 * C2) / det;
    result->Y = (A1 * C2 - A2 * C1) / det;
}

/* kate: bom off; indent-mode cstyle; indent-width 4; replace-trailing-space-save on; */