summaryrefslogtreecommitdiff
path: root/tikzit/src/common/Edge.h
blob: 64da1385a8a155c976f9a950d3b10f6d1027b4f7 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
//
//  Edge.h
//  TikZiT
//  
//  Copyright 2010 Aleks Kissinger. All rights reserved.
//  
//  
//  This file is part of TikZiT.
//  
//  TikZiT 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 3 of the License, or
//  (at your option) any later version.
//  
//  TikZiT is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with TikZiT.  If not, see <http://www.gnu.org/licenses/>.
//  


// Edge : store the data associated with an edge. Also, lazily compute
// bezier curve control points based on bend and the coordinates of 
// the endpoints.

#import "Node.h"
#import "EdgeStyle.h"

/*!
 @typedef    enum EdgeBendMode
 @brief      Indicates the type of edge bend.
 @var        EdgeBendModeBasic A basic, one-angle bend. Positive values will be interpreted
             as bend left, negative as bend right.
 @var        EdgeBendModeInOut A two-angle bend mode, using inAngle and outAngle.
 */
typedef enum {
	EdgeBendModeBasic,
	EdgeBendModeInOut
} EdgeBendMode;

/*!
 @class      Edge
 @brief      A graph edge, with associated bend and style data.
 @details    A graph edge, with associated bend and style data. This class
             also contains methods for computing the bezier control points
             and the midpoint of the curve.
 */
@interface Edge : NSObject<NSCopying> {
	Node *source;
	Node *target;
	Node *edgeNode;
	int bend;
	int inAngle, outAngle;
	EdgeBendMode bendMode;
	float weight;
    EdgeStyle *style;
	GraphElementData *data;
    NSString *sourceAnchor;
    NSString *targetAnchor;
	
    // When set to YES, lazily create the edge node, and keep it around when set
    // to NO (at least until saved/loaded).
    BOOL hasEdgeNode;
	BOOL dirty;
	
	// these are all cached values computed from the above
	NSPoint src;
	NSPoint targ;
	NSPoint head;
	NSPoint tail;
	NSPoint cp1;
	NSPoint cp2;
	NSPoint mid;
    NSPoint midTan;
    NSPoint headTan;
    NSPoint tailTan;
}

/*!
 @property   data
 @brief      Associated edge data.
 */
@property (copy) GraphElementData *data;

// KVC methods
- (void) insertObject:(GraphElementProperty*)gep
		inDataAtIndex:(NSUInteger)index;
- (void) removeObjectFromDataAtIndex:(NSUInteger)index;
- (void) replaceObjectInDataAtIndex:(NSUInteger)index
						 withObject:(GraphElementProperty*)gep;

/*!
 @property   style
 @brief      Edge style.
 */
@property (retain) EdgeStyle *style;

/*!
 @property   source
 @brief      Source node.
 */
@property (retain) Node *source;

/*!
 @property   target
 @brief      Target node.
 */
@property (retain) Node *target;

/*!
 @property   edgeNode
 @brief      A node attached to this edge, as in a label or tick.
 */
@property (retain) Node *edgeNode;

/*!
 @property   sourceAnchor
 @brief      The source node anchor point, as in north or center.
 */
@property (copy) NSString *sourceAnchor;

/*!
 @property   targetAnchor
 @brief      The target node anchor point, as in north or center.
 */
@property (copy) NSString *targetAnchor;

/*!
 @property   hasEdgeNode
 @brief      A read/write property. When set to true, a new edge node is actually constructed.
*/
@property (assign) BOOL hasEdgeNode;

/*!
 @property   bend
 @brief      The degrees by which the edge bends.
 */
@property (assign) int bend;

/*!
 @property   weight
 @brief      How close the edge will pass to control points.
 */
@property (assign) float weight;

/*!
 @property   inAngle
 @brief      The angle by which the edge enters its target.
 */
@property (assign) int inAngle;

/*!
 @property   outAngle
 @brief      The angle by which the edge leaves its target.
 */
@property (assign) int outAngle;

/*!
 @property   bendMode
 @brief      The mode of the edge bend. Either simple bend in in/out style.
 */
@property (assign) EdgeBendMode bendMode;

/*!
 @property   head
 @brief      The starting point of the edge.
 @detail     This value is computed based on the source, target and
             either bend or in/out angles.  It is where the edge
			 makes contact with the source node.
 */
@property (readonly) NSPoint head;

/*!
 @property   tail
 @brief      The ending point of the edge.
 @detail     This value is computed based on the source, target and
             either bend or in/out angles.  It is where the edge
			 makes contact with the target node.
 */
@property (readonly) NSPoint tail;

/*!
 @property   cp1
 @brief      The first control point of the edge.
 @detail     This value is computed based on the source, target and
             either bend or in/out angles.
 */
@property (readonly) NSPoint cp1;

/*!
 @property   cp2
 @brief      The second control point of the edge.
 @detail     This value is computed based on the source, target and
             either bend or in/out angles.
 */
@property (readonly) NSPoint cp2;

/*!
 @property   mid
 @brief      The midpoint of the curve. Computed from the source, target, and control points.
 */
@property (readonly) NSPoint mid;

/*!
 @property   mid_tan
 @brief      The second point of a line tangent to the midpoint. (The first is the midpoint itself.)
 */
@property (readonly) NSPoint midTan;

/*!
 @property   left_normal
 @brief      The second point in a line perp. to the edge coming from mid-point. (left side)
 */
@property (readonly) NSPoint leftNormal;

/*!
 @property   left_normal
 @brief      The second point in a line perp. to the edge coming from mid-point. (right side)
 */
@property (readonly) NSPoint rightNormal;

@property (readonly) NSPoint headTan;

/*!
 @property   leftHeadNormal
 */
@property (readonly) NSPoint leftHeadNormal;

/*!
 @property   rightHeadNormal
 */
@property (readonly) NSPoint rightHeadNormal;

@property (readonly) NSPoint tailTan;

/*!
 @property   leftTailNormal
 */
@property (readonly) NSPoint leftTailNormal;

/*!
 @property   rightTailNormal
 */
@property (readonly) NSPoint rightTailNormal;

/*!
 @property   isSelfLoop
 @brief      Returns YES if this edge is a self loop.
 */
@property (readonly) BOOL isSelfLoop;

/*!
 @property   isStraight
 @brief      Returns YES if this edge can be drawn as a straight line (as opposed to a bezier curve).
 */
@property (readonly) BOOL isStraight;


/*!
 @brief      Construct a blank edge.
 @result     An edge.
 */
- (id)init;

/*!
 @brief      Construct an edge with the given source and target.
 @param      s the source node.
 @param      t the target node.
 @result     An edge.
 */
- (id)initWithSource:(Node*)s andTarget:(Node*)t;

/*!
 @brief      Recompute the control points and midpoint.
 */
- (void)updateControls;

/*!
 @brief      Push edge properties back into its <tt>GraphElementData</tt>.
 */
- (void)updateData;

/*!
 @brief      Set edge properties from fields in <tt>GraphElementData</tt>.
 */
- (void)setAttributesFromData;

/*!
 @brief      Use data.style to find and attach the <tt>EdgeStyle</tt> object from the given array.
 */
- (BOOL)attachStyleFromTable:(NSArray*)styles;

/*!
 @brief      Convert the bend angle to an inAngle and outAngle.
 */
- (void)convertBendToAngles;

/*!
 @brief      Set the bend angle to the average of the in and out angles.
 */
- (void)convertAnglesToBend;

/*!
 @brief      Update this edge to look just like the given edge.
 @param      e an edge to mimic.
 */
- (void)setPropertiesFromEdge:(Edge *)e;

/*!
 @brief      Get a bounding rect for this edge.
 @detail     Note that this may not be a tight bound.
 */
- (NSRect)boundingRect;

/*!
 @brief      Moves the first control point, updating edge properties as necessary
 @detail     This will move a control point and adjust the weight and
             bend (or outAngle) to fit.

             A courseness can be specified for both the weight and the
             bend, allowing them to be constrained to certain values.  For
             example, passing 10 as the bend courseness will force the bend
             to be a multiple of 5.  Passing 0 for either of these will
             cause the relevant value to be unconstrained.
 @param      point  the new position of the control point
 @param      wc  force the weight to be a multiple of this value (unless 0)
 @param      bc  force the bend (or outAngle) to be a multiple of this value (unless 0)
 @param      link  when in EdgeBendModeInOut, change both the in and out angles at once
 */
- (void) moveCp1To:(NSPoint)point withWeightCourseness:(float)wc andBendCourseness:(int)bc forceLinkControlPoints:(BOOL)link;

/*!
 @brief      Moves the first control point, updating edge properties as necessary
 @detail     This will move a control point and adjust the weight and
             bend (or outAngle) to fit.

             The same as moveCp1To:point withWeightCourseness:0.0f andBendCourseness:0 forceLinkControlPoints:No
 @param      point  the new position of the control point
 @param      wc  force the weight to be a multiple of this value (unless 0)
 @param      bc  force the bend (or outAngle) to be a multiple of this value (unless 0)
 @param      link  when in EdgeBendModeInOut, change both the in and out angles at once
 */
- (void) moveCp1To:(NSPoint)point;

/*!
 @brief      Moves the first control point, updating edge properties as necessary
 @detail     This will move a control point and adjust the weight and
             bend (or inAngle) to fit.

             A courseness can be specified for both the weight and the
             bend, allowing them to be constrained to certain values.  For
             example, passing 10 as the bend courseness will force the bend
             to be a multiple of 5.  Passing 0 for either of these will
             cause the relevant value to be unconstrained.
 @param      point  the new position of the control point
 @param      wc  force the weight to be a multiple of this value (unless 0)
 @param      bc  force the bend (or inAngle) to be a multiple of this value (unless 0)
 @param      link  when in EdgeBendModeInOut, change both the in and out angles at once
 */
- (void) moveCp2To:(NSPoint)point withWeightCourseness:(float)wc andBendCourseness:(int)bc forceLinkControlPoints:(BOOL)link;

/*!
 @brief      Moves the first control point, updating edge properties as necessary
 @detail     This will move a control point and adjust the weight and
             bend (or inAngle) to fit.

             The same as moveCp2To:point withWeightCourseness:0.0f andBendCourseness:0 forceLinkControlPoints:No
 @param      point  the new position of the control point
 */
- (void) moveCp2To:(NSPoint)point;

/*!
 @brief      Reverse edge direction, updating bend/inAngle/outAngle/etc
 */
- (void)reverse;

/*!
 @brief      Factory method to make a blank edge.
 @result     An edge.
 */
+ (Edge*)edge;

/*!
 @brief      Factory method to make an edge with the given source and target.
 @param      s a source node.
 @param      t a target node.
 @result     An edge.
 */
+ (Edge*)edgeWithSource:(Node*)s andTarget:(Node*)t;

@end

// vi:ft=objc:noet:ts=4:sts=4:sw=4