1/* VLine3D.m
2 * 3-D line object
3 *
4 * Copyright (C) 1996-2011 by vhf interservice GmbH
5 * Author:   Georg Fleischmann
6 *
7 * created:  1996-01-29
8 * modified: 2011-04-04 (lineWithPoints: added)
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the vhf Public License as
12 * published by vhf interservice GmbH. Among other things, the
13 * License requires that the copyright notices and this notice
14 * be preserved on all copies.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 * See the vhf Public License for more details.
20 *
21 * You should have received a copy of the vhf Public License along
22 * with this program; see the file LICENSE. If not, write to vhf.
23 *
24 * vhf interservice GmbH, Im Marxle 3, 72119 Altingen, Germany
25 * eMail: info@vhf.de
26 * http://www.vhf.de
27 */
28
29#include <AppKit/AppKit.h>
30#include "VLine3D.h"
31#include "../DocView.h"
32
33@interface VLine3D(PrivateMethods)
34@end
35
36@implementation VLine3D
37
38+ (VLine3D*)line3D
39{
40    return [[VLine3D new] autorelease];
41}
42+ (VLine3D*)line3DWithPoints:(V3Point)pl0 :(V3Point)pl1
43{   VLine3D *line = [[[VLine3D allocWithZone:[self zone]] init] autorelease];
44
45    [line setVertices3D:pl0 :pl1];
46    return line;
47}
48
49
50/* set our vertices
51 */
52- (void)setVertices3D:(V3Point)pv0 :(V3Point)pv1
53{
54    p0 = NSMakePoint(pv0.x, pv0.y);
55    z0 = pv0.z;
56    p1 = NSMakePoint(pv1.x, pv1.y);
57    z1 = pv1.z;
58    dirty = YES;
59}
60
61/* deep copy
62 *
63 * created:  2001-02-15
64 * modified:
65 */
66- copy
67{   VLine3D *line3d = [[VLine3D allocWithZone:[self zone]] init];
68
69    [line3d setWidth:width];
70    [line3d setSelected:isSelected];
71    [line3d setLocked:NO];
72    [line3d setColor:color];
73    [line3d setVertices:p0 :p1];
74    [line3d setZLevel:z0 :z1];
75    return line3d;
76}
77
78/* set our vertices
79 */
80- (void)setZLevel:(float)zv0 :(float)zv1
81{
82    z0 = zv0;
83    z1 = zv1;
84}
85
86/*
87 * return our vertices
88 */
89- (void)getZLevel:(float*)zv0 :(float*)zv1
90{
91    *zv0 = z0;
92    *zv1 = z1;
93}
94
95- (void)setLength:(float)length
96{   double	axy, az, dx, dy, dz, xyLength, oxyLength = sqrt(SqrDistPoints(p0, p1));
97    double	oLength = [self length];
98
99    dz = z1 - z0;
100    az = Asin(dz / oLength);
101    if ( az < 0.0 )	az += 360.0;
102
103    dz = length * Sin(az);
104    xyLength = sqrt(length*length - dz*dz);
105    if (Cos(az)<0.0) xyLength = -xyLength;
106
107    dy = p1.y - p0.y;
108    axy = Asin(dy / oxyLength);
109    if ( axy < 0.0 )	axy += 360.0;
110    if ( p1.x < p0.x )	axy = 180.0 - axy;
111    if ( axy < 0.0 )	axy += 360.0;
112
113    dy = xyLength * Sin(axy);
114    dx = sqrt( xyLength*xyLength - dy*dy );
115    if (Cos(axy)<0.0) dx = -dx;
116
117    p1 = NSMakePoint(p0.x+dx, p0.y+dy);
118    dx = p1.x - p0.x;
119    dy = p1.y - p0.y;
120    z1 = z0 + dz;
121    dirty = YES;
122}
123- (float)length
124{   float	lxy = sqrt(SqrDistPoints(p0, p1)), dz = z1-z0;
125
126    return sqrt(lxy*lxy+dz*dz);
127}
128
129- (void)drawWithPrincipal:principal
130{   float	defaultWidth = [NSBezierPath defaultLineWidth];
131
132    [super drawWithPrincipal:principal];
133
134    [NSBezierPath setDefaultLineWidth:(width > 0.0) ? width : defaultWidth];
135    [NSBezierPath setDefaultLineCapStyle: NSRoundLineCapStyle];
136    [NSBezierPath setDefaultLineJoinStyle:NSRoundLineJoinStyle];
137    [NSBezierPath strokeLineFromPoint:p0 toPoint:p1];
138    [NSBezierPath setDefaultLineWidth:defaultWidth];
139
140    if ([principal showDirection])
141        [self drawDirectionAtScale:[principal scaleFactor]];
142}
143
144- (void)encodeWithCoder:(NSCoder *)aCoder
145{
146    [super encodeWithCoder:aCoder];
147    [aCoder encodeValuesOfObjCTypes:"ff", &z0, &z1];
148}
149- (id)initWithCoder:(NSCoder *)aDecoder
150{   int	version;
151
152    [super initWithCoder:aDecoder];
153    version = [aDecoder versionForClassName:@"VLine3D"];
154    [aDecoder decodeValuesOfObjCTypes:"ff", &z0, &z1];
155
156    return self;
157}
158
159/* archiving with property list
160 */
161- (id)propertyList
162{   NSMutableDictionary	*plist = [super propertyList];
163
164    [plist setObject:propertyListFromFloat(z0) forKey:@"z0"];
165    [plist setObject:propertyListFromFloat(z1) forKey:@"z1"];
166    return plist;
167}
168- (id)initFromPropertyList:(id)plist inDirectory:(NSString *)directory
169{
170    [super initFromPropertyList:plist inDirectory:directory];
171    z0 = [plist floatForKey:@"z0"];
172    z1 = [plist floatForKey:@"z1"];
173    return self;
174}
175
176
177- (void)dealloc
178{
179    [super dealloc];
180}
181
182@end
183