summaryrefslogtreecommitdiffstats
path: root/src/mechanics/mechanicsitem.cpp
blob: c02cfa445470424859421374befff1291d0e8255 (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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/***************************************************************************
 *   Copyright (C) 2004-2005 by David Saxton                               *
 *   david@bluehaze.org                                                    *
 *                                                                         *
 *   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.                                   *
 ***************************************************************************/

#include "itemdocumentdata.h"
#include "mechanicsitem.h"
#include "mechanicsdocument.h"

#include <kdebug.h>
#include <tdelocale.h>
#include <tqbitarray.h>
#include <tqpainter.h>
#include <tqwmatrix.h>
#include <cmath>

/**
@returns an angle between 0 and 2 pi
*/
double normalizeAngle( double angle )
{
	if ( angle < 0 )
		angle += 6.2832*(std::ceil(-angle));
	
	return angle - 6.2832*std::floor(angle/6.2832);
}

MechanicsItem::MechanicsItem( MechanicsDocument *mechanicsDocument, bool newItem, const TQString &id )
	: Item( mechanicsDocument, newItem, id )
{
	p_mechanicsDocument = mechanicsDocument;
	m_selectionMode = MechanicsItem::sm_move;
	
	createProperty( "mass", Variant::Type::Double );
	property("mass")->setCaption( i18n("Mass") );
	property("mass")->setUnit("g");
	property("mass")->setValue(10.0);
	property("mass")->setMinValue(1e-3);
	property("mass")->setMaxValue(1e12);
	property("mass")->setAdvanced(true);
	
	createProperty( "moi", Variant::Type::Double );
	property("moi")->setCaption( i18n("Moment of Inertia") );
	property("moi")->setUnit("gm");
	property("moi")->setValue(0.01);
	property("moi")->setMinValue(1e-3);
	property("moi")->setMaxValue(1e12);
	property("moi")->setAdvanced(true);
	
	setZ(ItemDocument::Z::Item);
	setAnimated(true);
	p_mechanicsDocument->registerItem(this);
}


MechanicsItem::~MechanicsItem()
{
}


int MechanicsItem::rtti() const
{
	return ItemDocument::RTTI::MechanicsItem;
}


void MechanicsItem::setSelectionMode( SelectionMode sm )
{
	if ( sm == m_selectionMode )
		return;
	
	m_selectionMode = sm;
}


void MechanicsItem::setSelected( bool yes )
{
	if ( yes == isSelected() )
		return;
	
	if (!yes)
		// Reset the selection mode
		m_selectionMode = MechanicsItem::sm_resize;
	
	Item::setSelected(yes);
}


void MechanicsItem::dataChanged()
{
	Item::dataChanged();
	m_mechanicsInfo.mass = dataDouble("mass");
	m_mechanicsInfo.momentOfInertia = dataDouble("moi");
	updateMechanicsInfoCombined();
}


PositionInfo MechanicsItem::absolutePosition() const
{
	MechanicsItem *parentMechItem = dynamic_cast<MechanicsItem*>((Item*)(p_parentItem));
	if (parentMechItem)
		return parentMechItem->absolutePosition() + m_relativePosition;
	
	return m_relativePosition;
}


void MechanicsItem::reparented( Item *oldItem, Item *newItem )
{
	MechanicsItem *oldMechItem = dynamic_cast<MechanicsItem*>(oldItem);
	MechanicsItem *newMechItem = dynamic_cast<MechanicsItem*>(newItem);
	
	if (oldMechItem)
	{
		m_relativePosition = oldMechItem->absolutePosition() + m_relativePosition;
		disconnect( oldMechItem, TQT_SIGNAL(moved()), this, TQT_SLOT(parentMoved()) );
	}
	
	if (newMechItem)
	{
		m_relativePosition = m_relativePosition - newMechItem->absolutePosition();
		connect( newMechItem, TQT_SIGNAL(moved()), this, TQT_SLOT(parentMoved()) );
	}
	
	updateCanvasPoints();
}


void MechanicsItem::childAdded( Item *child )
{
	MechanicsItem *mechItem = dynamic_cast<MechanicsItem*>(child);
	if (!mechItem)
		return;
	
	connect( mechItem, TQT_SIGNAL(updateMechanicsInfoCombined()), this, TQT_SLOT(childMoved()) );
	updateMechanicsInfoCombined();
}


void MechanicsItem::childRemoved( Item *child )
{
	MechanicsItem *mechItem = dynamic_cast<MechanicsItem*>(child);
	if (!mechItem)
		return;
	
	disconnect( mechItem, TQT_SIGNAL(updateMechanicsInfoCombined()), this, TQT_SLOT(childMoved()) );
	updateMechanicsInfoCombined();
}


void MechanicsItem::parentMoved()
{
	PositionInfo absPos = absolutePosition();
	Item::moveBy( absPos.x() - x(), absPos.y() - y() );
	updateCanvasPoints();
	emit moved();
}


void MechanicsItem::updateCanvasPoints()
{
	const TQRect ipbr = m_itemPoints.boundingRect();
	
	double scalex = double(m_sizeRect.width()) / double(ipbr.width());
	double scaley = double(m_sizeRect.height()) / double(ipbr.height());
	
	PositionInfo abs = absolutePosition();
	
	TQWMatrix m;
	m.rotate(abs.angle()*57.29577951308232);
	m.translate( m_sizeRect.left(), m_sizeRect.top() );
	m.scale( scalex, scaley );
	m.translate( -int(ipbr.left()), -int(ipbr.top()) );
	setPoints( m.map(m_itemPoints) );
	
	TQRect tempt = m.mapRect(ipbr);
}


void MechanicsItem::rotateBy( double dtheta )
{
	m_relativePosition.rotate(dtheta);
	updateCanvasPoints();
	updateMechanicsInfoCombined();
	emit moved();
}


void MechanicsItem::moveBy( double dx, double dy )
{
	m_relativePosition.translate( dx, dy );
	Item::moveBy( m_relativePosition.x() - x(), m_relativePosition.y() - y() );
	emit moved();
}


void MechanicsItem::updateMechanicsInfoCombined()
{
	m_mechanicsInfoCombined = m_mechanicsInfo;
	
	double mass_x = 0.;
	double mass_y = 0.;
	
	const ItemList::const_iterator end = m_children.end();
	for ( ItemList::const_iterator it = m_children.begin(); it != end; ++it )
	{
		MechanicsItem *child = dynamic_cast<MechanicsItem*>((Item*)*it);
		if (child)
		{
			CombinedMechanicsInfo *childInfo = child->mechanicsInfoCombined();
			const PositionInfo relativeChildPosition = child->relativePosition();
			
			double mass = childInfo->mass;
// 			double angle = relativeChildPosition.angle();
			double dx = relativeChildPosition.x() /*+ cos(angle)*childInfo->m_x - sin(angle)*childInfo->m_y*/;
			double dy = relativeChildPosition.y() /*+ sin(angle)*childInfo->m_x + cos(angle)*childInfo->m_y*/;
			
			m_mechanicsInfoCombined.mass += mass;
			mass_x += mass * dx;
			mass_y += mass * dy;
			
			double length_squared = dx*dx + dy*dy;
			m_mechanicsInfoCombined.momentOfInertia += length_squared * childInfo->momentOfInertia;
		}
	}
	
	m_mechanicsInfoCombined.x = mass_x / m_mechanicsInfoCombined.mass;
	m_mechanicsInfoCombined.y = mass_y / m_mechanicsInfoCombined.mass;
}


ItemData MechanicsItem::itemData() const
{
	ItemData itemData = Item::itemData();
	itemData.angleDegrees = m_relativePosition.angle()*57.29577951308232;
	return itemData;
}


bool MechanicsItem::mousePressEvent( const EventInfo &eventInfo )
{
	Q_UNUSED(eventInfo);
	return false;
}


bool MechanicsItem::mouseReleaseEvent( const EventInfo &eventInfo )
{
	Q_UNUSED(eventInfo);
	return false;
}


bool MechanicsItem::mouseDoubleClickEvent( const EventInfo &eventInfo )
{
	Q_UNUSED(eventInfo);
	return false;
}


bool MechanicsItem::mouseMoveEvent( const EventInfo &eventInfo )
{
	Q_UNUSED(eventInfo);
	return false;
}


bool MechanicsItem::wheelEvent( const EventInfo &eventInfo )
{
	Q_UNUSED(eventInfo);
	return false;
}


void MechanicsItem::enterEvent()
{
}


void MechanicsItem::leaveEvent()
{
}


TQRect MechanicsItem::maxInnerRectangle( const TQRect &outerRect ) const
{
	TQRect normalizedOuterRect = outerRect.normalize();
	const double LEFT = normalizedOuterRect.left();
	const double TOP = normalizedOuterRect.top();
	const double X = normalizedOuterRect.width();
	const double Y = normalizedOuterRect.height();
	const double a = normalizeAngle(absolutePosition().angle());
	
	double left;
	double top;
	double width;
	double height;
	
// 	if ( can change width/height ratio )
	{
		double x1 = X*std::cos(a) - Y*std::sin(a);
		double y1 = X*std::sin(a) + Y*std::cos(a);
		double x2 = X*std::cos(a);
		double y2 = X*std::sin(a);
		double x3 = -Y*std::sin(a);
		double y3 = Y*std::cos(a);
		
		double xbig;/* = std::max( std::abs(x2-x3), std::abs(x1) );*/
		double ybig;/* = std::max( std::abs(y2-y3), std::abs(y1) );*/
		if ( (a - floor(a/6.2832)*6.2832) < 3.1416 )
		{
			xbig = std::abs(x3-x2);
			ybig = std::abs(y1);
		}
		else
		{
			xbig = std::abs(x1);
			ybig = std::abs(y3-y2);
		}
		
		width = X*(X/xbig);
		height = Y*(Y/ybig);
		
		top = -std::sin(a) * (LEFT + width*std::sin(a)) + std::cos(a)*TOP;
		left = std::cos(a) * (LEFT + width*std::sin(a)) + std::sin(a)*TOP;
	}
	
	return TQRect( int(left), int(top), int(width), int(height) );
}


void MechanicsItem::initPainter( TQPainter &p )
{
       PositionInfo absPos = absolutePosition();
       p.translate( absPos.x(), absPos.y() );
       // 57.29577951308232 is the number of degrees per radian.
       p.rotate( absPos.angle()*57.29577951308232 );
       p.translate( -absPos.x(), -absPos.y() );
}


void MechanicsItem::deinitPainter( TQPainter &p )
{
       PositionInfo absPos = absolutePosition();
       p.translate( absPos.x(), absPos.y() );
       // 57.29577951308232 is the number of degrees per radian.
       p.rotate( -absPos.angle()*57.29577951308232 );
       p.translate( -absPos.x(), -absPos.y() );
}





PositionInfo::PositionInfo()
{
	reset();
}


const PositionInfo PositionInfo::operator+( const PositionInfo &info )
{
	// Copy the child to a new position
	PositionInfo newInfo = info;
	
	// Translate the newInfo by our translation amount
	newInfo.translate( x(), y() );
	
	// Rotate the child about us
	newInfo.rotateAboutPoint( x(), y(), angle() );
	
	return newInfo;
}


const PositionInfo PositionInfo::operator-( const PositionInfo &info )
{

	PositionInfo newInfo = *this;
	
	newInfo.translate( -info.x(), -info.y() );
	newInfo.rotate( -info.angle() );
	
	return newInfo;
}


void PositionInfo::rotateAboutPoint( double x, double y, double angle )
{
	m_angle += angle;
	
	double newx = x + (m_x-x)*std::cos(angle) - (m_y-y)*std::sin(angle);
	double newy = y + (m_x-x)*std::sin(angle) + (m_y-y)*std::cos(angle);
	
	m_x = newx;
	m_y = newy;
}


void PositionInfo::reset()
{
	m_x = 0.;
	m_y = 0.;
	m_angle = 0.;
}



MechanicsInfo::MechanicsInfo()
{
	mass = 0.;
	momentOfInertia = 0.;
}
CombinedMechanicsInfo::CombinedMechanicsInfo()
	: MechanicsInfo()
{
	x = 0.;
	y = 0.;
}
CombinedMechanicsInfo::CombinedMechanicsInfo( const MechanicsInfo &info )
	: MechanicsInfo(info)
{
	x = 0.;
	y = 0.;
}


#include "mechanicsitem.moc"