-
Notifications
You must be signed in to change notification settings - Fork 5
/
crushbitmap.cpp
692 lines (598 loc) · 20.3 KB
/
crushbitmap.cpp
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
/*************************************************************************************
//
// LICENSE INFORMATION
//
// BCreator(tm)
// Software for the control of the 3D Printer, "B9Creator"(tm)
//
// Copyright 2011-2012 B9Creations, LLC
// B9Creations(tm) and B9Creator(tm) are trademarks of B9Creations, LLC
//
// This file is part of B9Creator
//
// B9Creator 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.
//
// B9Creator 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 B9Creator . If not, see <http://www.gnu.org/licenses/>.
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
*************************************************************************************/
#include "crushbitmap.h"
#include <QDataStream>
#include <QtDebug>
#include <QtCore/qmath.h>
#include <QPainter>
////////////////////////////////////////////////////////////
//
// SimpleSupport functions
//
///////////////////////////////////////////////////////////
////////////////////////////////////////////////////////
// Render the support to the image
void SimpleSupport::draw(QImage* pImage) {
int ihalfSize = mSize/2;
float ihalfbase = mSize/1.73205081;
QPointF points[4];
QPainter tPainter(pImage);
tPainter.setPen(QColor(255,255,255));
tPainter.setBrush(QBrush(QColor(255,255,255)));
tPainter.setBackgroundMode(Qt::OpaqueMode);
switch(mType) {
case st_CIRCLE:
tPainter.drawEllipse(mPoint.x()-ihalfSize,mPoint.y()-ihalfSize,mSize,mSize);
break;
case st_SQUARE:
tPainter.fillRect(mPoint.x()-ihalfSize,mPoint.y()-ihalfSize,mSize,mSize,Qt::white);
break;
case st_TRIANGLE:
points[0].setX( mPoint.x()); points[0].setY(-ihalfSize + mPoint.y());
points[1].setX( ihalfbase + mPoint.x()); points[1].setY( ihalfSize + mPoint.y());
points[2].setX(-ihalfbase + mPoint.x()); points[2].setY( ihalfSize + mPoint.y());
tPainter.drawPolygon(points, 3);
break;
case st_DIAMOND:
points[0].setX( mPoint.x()); points[0].setY(-ihalfSize + mPoint.y());
points[1].setX( ihalfbase + mPoint.x()); points[1].setY( mPoint.y());
points[2].setX( mPoint.x()); points[2].setY( ihalfSize + mPoint.y());
points[3].setX(-ihalfbase + mPoint.x()); points[3].setY( mPoint.y());
tPainter.drawPolygon(points, 4);
break;
default:
break;
}
}
QImage SimpleSupport::getCursorImage(){
QImage cursor(32,32,QImage::Format_ARGB32);
cursor.fill(QColor(0,0,0,0));
draw(&cursor);
return cursor;
}
void SimpleSupport::streamOutSupport(QDataStream* pOut){
*pOut << mPoint << (quint32)mType << (quint32)mSize << (quint32)mStart << (quint32)mEnd;
}
void SimpleSupport::streamInSupport(QDataStream* pIn){
quint32 temp;
*pIn >> mPoint >> temp >> mSize >> mStart >> mEnd;
mType = (SupportType)temp;
}
////////////////////////////////////////////////////////////
//
// CrushedBitMap functions
//
///////////////////////////////////////////////////////////
CrushedBitMap::CrushedBitMap(QImage* pImage)
{
mIndex = 0;
uiWhitePixels = 0;
crushSlice(pImage);
m_bIsBaseLayer=false;
}
CrushedBitMap::CrushedBitMap(QPixmap* pPixmap)
{
mIndex = 0;
uiWhitePixels = 0;
crushSlice(pPixmap);
m_bIsBaseLayer=false;
}
bool CrushedBitMap::saveCrushedBitMap(const QString &fileName)
{
QFile file(fileName);
if (!file.open(QIODevice::WriteOnly))
return false;
QDataStream out(&file);
streamOutCMB(&out);
return true;
}
void CrushedBitMap::streamOutCMB(QDataStream* pOut)
{
*pOut << (quint32)uiWhitePixels << mExtents << mBitarray;
}
bool CrushedBitMap::loadCrushedBitMap(const QString &fileName)
{
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly))
return false;
QDataStream in(&file);
streamInCMB(&in);
return true;
}
void CrushedBitMap::streamInCMB(QDataStream* pIn)
{
*pIn >> uiWhitePixels >> mExtents >> mBitarray;
mIndex = 0; // Reset to start
m_iWidth = popBits(16);
m_iHeight = popBits(16);
mIndex = 0;
}
void CrushedBitMap::inflateSlice(QImage* pImage, int xOffset, int yOffset, bool bUseNaturalSize)
{
if(pImage == NULL) return; // No image to draw
// Not using the slice's natural size, so pImage should have some size greater than zero!
if(!bUseNaturalSize && (pImage->width()<=0 || pImage->height()<=0))
return;
mIndex = 0; // Reset to start
bool bCurColorIsWhite = false;
unsigned uCurrentPos = 0;
unsigned uImageSize = 0;
unsigned uData = 0;
int iKey;
if(!m_bIsBaseLayer){
// not a fake base layer, so inflate the natural width and height info and the first color (white or not?)
m_iWidth = popBits(16);
m_iHeight = popBits(16);
bCurColorIsWhite = (popBits(1)==1);
uImageSize = m_iWidth * m_iHeight;
}
if(bUseNaturalSize)
{
if(m_iWidth<1 || m_iHeight<1)
{
m_iWidth = pImage->width();
m_iHeight = pImage->height();
*pImage = QImage(m_iWidth,m_iHeight,QImage::Format_ARGB32_Premultiplied);
pImage->fill(qRgba(0,0,0,255));
return;
}
// Using the slice's natural size, so we create a new QImage of this size and fill it with black pixels
*pImage = QImage(m_iWidth,m_iHeight,QImage::Format_ARGB32_Premultiplied);
pImage->fill(qRgba(0,0,0,255));
}
// note, WinXXX will equal SliceXXX if natural sized
float WinWidth = pImage->width()/2.0;
float WinHeight= pImage->height()/2.0;
float SliceWidth = m_iWidth/2.0;
float SliceHeight = m_iHeight/2.0;
// Find centering offsets (zero if equal sizes
int widthOff = (int)(WinWidth - SliceWidth);
int heightOff = (int)(WinHeight - SliceHeight);
// Find adjusted offsets (from centered) based on input parameters
m_xOffset = widthOff + xOffset;
m_yOffset = heightOff + yOffset;
if(m_bIsBaseLayer) return; // If it's a base layer, we're done (no "non-support" image to inflate)
// inflate the first pixel run info (uData = run length)
iKey = popBits(5);
uData = popBits(iKey+1);
while((uData > 0) && (uCurrentPos < uImageSize)) { //if uData <= 0, we're done. if uCurrentPos >= uImage Size we've set the last pixel
for(unsigned ui=0; ui<uData; ui++) {
if(bCurColorIsWhite) setWhiteImagePixel(pImage, uCurrentPos); // Set the corresponding pixel in pImage
uCurrentPos++; //Note we could exceed uImageSize but setWhiteImagePixel above will safely ignore this error
}
// pop length of next run of pixels
iKey = popBits(5);
uData = popBits(iKey+1);
// toggle current color
if(bCurColorIsWhite) bCurColorIsWhite = false; else bCurColorIsWhite = true;
}
return;
}
void CrushedBitMap::setWhiteImagePixel(QImage* pImage, unsigned uCurPos)
{
// define a white pixel
QRgb whitePixel = qRgb(255,255,255);
// determine it's location x,y
int x, y;
y = uCurPos / m_iWidth;
x = uCurPos - y*m_iWidth;
x += m_xOffset;
y += m_yOffset;
// set it to white, if valid x,y
if(x>=0 && y>=0 && x<pImage->width() && y<pImage->height()) {
pImage->setPixel(x,y,whitePixel);
}
}
bool CrushedBitMap::crushSlice(QPixmap* pPixmap)
{
QImage img = pPixmap->toImage();
bool bResult = crushSlice(&img);
return bResult;
}
bool CrushedBitMap::crushSlice(QImage* pImage)
{
if(pImage == NULL) return false;
m_iWidth=pImage->width();
m_iHeight=pImage->height();
unsigned uCurrentPos = 0;
unsigned uImageSize = m_iWidth * m_iHeight;
unsigned uData = 0;
int iKey;
uiWhitePixels = 0;
bool bCurColorIsWhite = pixelIsWhite(pImage, 0);
// reset the bit array
if (mBitarray.size()>0) mBitarray.resize(0);
mIndex = 0;
// reset extents
mExtents.setBottomRight(QPoint(0,0));
mExtents.setTopLeft(QPoint(pImage->width(),pImage->height()));
// push the image width and height
pushBits(pImage->width(),16);
pushBits(pImage->height(),16);
// push the first colorbit (0 or 1)
pushBits(bCurColorIsWhite, 1);
// loop through all the pixels in the image
do {
uData = 0;
bCurColorIsWhite = pixelIsWhite(pImage, uCurrentPos); //returns true if pixel is white, updates extents
// count the pixels until the color changes
while ((uCurrentPos < uImageSize) && (bCurColorIsWhite == pixelIsWhite(pImage, uCurrentPos))) {
uCurrentPos++;
uData++;
if(bCurColorIsWhite)uiWhitePixels++;
}
// store the count
iKey = computeKeySize(uData);
if(iKey<0) return false;
pushBits(iKey, 5);
pushBits(uData, iKey+1);
} while (uCurrentPos < uImageSize);
return true;
}
bool CrushedBitMap::pixelIsWhite(QImage* pImage, unsigned uCurPos)
{
// define a black pixel
//QRgb blackPixel = QColor(0,0,0).rgb();
int x, y;
y = uCurPos / pImage->width();
x = uCurPos - y*pImage->width();
// Import "mostly black" pixels as black, else white. This helps clean up .jpg compression artifacts.
QColor c = pImage->pixel(x,y);
if(c.red()<32 && c.blue()<32 && c.green()<32) return false;
if(x < mExtents.left() ) mExtents.setLeft(x);
if(x > mExtents.right() ) mExtents.setRight(x);
if(y > mExtents.bottom()) mExtents.setBottom(y);
if(y < mExtents.top() ) mExtents.setTop(y);
return true;
}
int CrushedBitMap::computeKeySize(unsigned uData)
{
for (int iKey=0; iKey<32; iKey++)
if(uData <= qPow(2.0, iKey))
return iKey;
return -1;
}
void CrushedBitMap::pushBits(int iValue, int iBits)
{
// Pushes the iBits least significant bits of iValue into the bit array
// iBits should always be smaller than 33
if (mIndex + iBits > mBitarray.size()) mBitarray.resize(mIndex + iBits);
for (int i=iBits-1; i>=0; i--) {
mBitarray.setBit(mIndex, iValue & (int)qPow(2.0,i));
mIndex++;
}
}
int CrushedBitMap::popBits(int iBits)
{
// Pops iBits from the array (up to 32) returns -1 if iBits>32 or end of array reached
int iReturn = -1;
if (mIndex + iBits <= mBitarray.size() && iBits<33){
iReturn = 0;
for (int i=iBits-1; i>=0; i--) {
iReturn = (iReturn<<1) + mBitarray.testBit(mIndex);
mIndex++;
}
}
return iReturn;
}
bool CrushedBitMap::isWhitePixel(QPoint qPoint){
// inflate to qPoint, report if white pixel
if(m_bIsBaseLayer) return false;
bool bCurColorIsWhite = false;
unsigned uCurrentPos = 0;
unsigned uImageSize = 0;
unsigned uData = 0;
int iKey;
mIndex = 0; // Reset to start
m_iWidth = popBits(16);
m_iHeight = popBits(16);
bCurColorIsWhite = (popBits(1)==1);
uImageSize = m_iWidth * m_iHeight;
iKey = popBits(5);
uData = popBits(iKey+1);
int x, y;
while((uData > 0) && (uCurrentPos < uImageSize)) {
for(unsigned ui=0; ui<uData; ui++) {
y = (uCurrentPos / m_iWidth) + m_yOffset;
x = (uCurrentPos - y*m_iWidth) + m_xOffset;
if(qPoint == QPoint(x,y)) return bCurColorIsWhite;
uCurrentPos++;
}
// pop length of next run of pixels
iKey = popBits(5);
uData = popBits(iKey+1);
// toggle current color
if(bCurColorIsWhite) bCurColorIsWhite = false; else bCurColorIsWhite = true;
}
return false;
}
////////////////////////////////////////////////////////////
//
// CrushPrintJob functions
//
///////////////////////////////////////////////////////////
CrushedPrintJob::CrushedPrintJob() {
clearAll();
}
CrushedBitMap* CrushedPrintJob::getCBMSlice(int i) {
// i is zero based index, values of 0 to mSlices.size()-1 inclusive
if(i >= mBase + mSlices.size()) return NULL;
if(i>=mBase) return &mSlices[i-mBase]; //mSlices[] does not store blank base offset layers, we fake those by always returning the same mBaseLayer CBM
mBaseLayer.setWidth(m_Width);
mBaseLayer.setHeight(m_Height);
mBaseLayer.setIsBaseLayer(true);
return &mBaseLayer;
}
void CrushedPrintJob::streamOutCPJ(QDataStream* pOut)
{
int i;
*pOut << mSlices.size();
// Loop throuh all slices and save them
for(i=0; i<mSlices.size();i++)
mSlices[i].streamOutCMB(pOut);
*pOut << mSupports.size();
// Loop throuh all supports and save them
for(i=0; i<mSupports.size();i++)
mSupports[i].streamOutSupport(pOut);
}
void CrushedPrintJob::streamInCPJ(QDataStream* pIn)
{
CrushedBitMap* pCBM;
mTotalWhitePixels = 0;
mSlices.clear();
mSupports.clear();
int i, iTotal;
*pIn >> iTotal;
// reset extents
mJobExtents.setBottomRight(QPoint(0,0));
mJobExtents.setTopLeft(QPoint(65535,65535));
m_Width=0;m_Height=0;
// Read in all slices.
for(i=0; i<iTotal;i++){
pCBM = new CrushedBitMap();
addCBM(*pCBM);
mSlices[i].streamInCMB(pIn);
mTotalWhitePixels += mSlices[i].getWhitePixels();
QRect tExtent = mSlices[i].getExtents();
if(mSlices[i].getHeight()<0 || mSlices[i].getWidth()<0)continue;
if(mSlices[i].getExtents().left() < mJobExtents.left() ) mJobExtents.setLeft( mSlices[i].getExtents().left());
if(mSlices[i].getExtents().right() > mJobExtents.right() ) mJobExtents.setRight( mSlices[i].getExtents().right());
if(mSlices[i].getExtents().bottom() > mJobExtents.bottom()) mJobExtents.setBottom(mSlices[i].getExtents().bottom());
if(mSlices[i].getExtents().top() < mJobExtents.top() ) mJobExtents.setTop( mSlices[i].getExtents().top());
if(m_Width<mSlices[i].getWidth())m_Width=mSlices[i].getWidth();
if(m_Height<mSlices[i].getHeight())m_Height=mSlices[i].getHeight();
}
// Initialize the generic base layer CBM for use with all "base" layers
mBaseLayer.setWidth(m_Width);
mBaseLayer.setHeight(m_Height);
mBaseLayer.setIsBaseLayer(true);
// Read in all supports
*pIn >> iTotal;
for(i=0; i<iTotal;i++) {
mSupports.append(SimpleSupport());
mSupports[i].streamInSupport(pIn);
}
}
bool CrushedPrintJob::loadCPJ(QFile* pFile)
{
if(!pFile->open(QIODevice::ReadOnly))
return false;
QDataStream in(pFile);
QString version;
double xy, z;
in >> version;
int iVersion = version.toInt();
if(iVersion == 1)
{
clearAll();
mVersion = version;
in >> mName;
in >> mDescription;
in >> xy;
in >> z;
in >> mBase;
in >> mFilled;
in >> mReserved3;
in >> mReserved2;
in >> mReserved1;
}
else return false; // unknown version
// qDebug() << "xy pix" << xy;
mXYPixel=QString::number(xy); mZLayer=QString::number(z);
streamInCPJ(&in);
pFile->close();
return true;
}
bool CrushedPrintJob::saveCPJ(QFile* pFile)
{
mVersion = "1"; // Update this and loadCPJ if changed!
if (!pFile->open(QIODevice::WriteOnly))
return false;
QDataStream out(pFile);
// Current Version "1"
out << mVersion << mName << mDescription << (qreal)mXYPixel.toDouble() << (qreal)mZLayer.toDouble() << (qint32)mBase << (qint32)mFilled << (QString)"Reserved3"<< (QString)"Reserved2"<< (QString)"Reserved1";
streamOutCPJ(&out);
pFile->close();
return true;
}
uint CrushedPrintJob::getTotalWhitePixels(int iFirst, int iLast)
{
uint iTotal = 0;
for(int i=iFirst; i<=iLast; i++)
if(i>0 && i<mSlices.size())
iTotal += mSlices[i].getWhitePixels();
return iTotal;
}
void CrushedPrintJob::clearAll(int iLayers) {
mTotalWhitePixels=0;
mBase=0;
mFilled=0;
mShowSupports=false;
m_Width=0; m_Height=0;
mJobExtents.setBottomRight(QPoint(0,0));
mJobExtents.setTopLeft(QPoint(65535,65535));
DeleteAllSupports();
mSlices.clear();
if(iLayers == 0) return;
CrushedBitMap *pCBM;
for(int i=0; i<iLayers; i++){
pCBM = new CrushedBitMap();
mSlices.append(*pCBM);
}
}
void CrushedPrintJob::inflateCurrentSlice(QImage* pImage, int xOffset, int yOffset, bool bUseNaturalSize)
{
inflateSlice(m_CurrentSlice, pImage, xOffset, yOffset, bUseNaturalSize);
}
void CrushedPrintJob::inflateSlice(int sliceIndx, QImage* pImage, int xOffset, int yOffset, bool bUseNaturalSize) {
float WinWidth;
float WinHeight;
float SliceWidth;
float SliceHeight;
int widthOff;
int heightOff;
SimpleSupport sSimple;
if(sliceIndx < 0 || sliceIndx >= getTotalLayers()) return;
getCBMSlice(sliceIndx)->inflateSlice(pImage, xOffset, yOffset, bUseNaturalSize);
//Todo Render filled Extent && Supports
if(mShowSupports){
WinWidth = pImage->width()/2.0;
WinHeight= pImage->height()/2.0;
SliceWidth = m_Width/2.0;
SliceHeight = m_Height/2.0;
widthOff = (int)(WinWidth - SliceWidth);
heightOff = (int)(WinHeight - SliceHeight);
xOffset = widthOff + xOffset;
yOffset = heightOff + yOffset;
QRect rOffset = mJobExtents;
rOffset.moveCenter(rOffset.center() + QPoint(xOffset, yOffset));
if(sliceIndx < mFilled){
// Render extents if filled layer
QPainter tPainter(pImage);
tPainter.setPen(QColor(255,255,255));
tPainter.setBrush(QBrush(QColor(255,255,255)));
tPainter.setBackgroundMode(Qt::OpaqueMode);
tPainter.fillRect(rOffset,Qt::white);
}
else {
// Render Supports
//Loop through supports list, if current slice has support, draw it
for(int i=0; i<mSupports.size(); i++){
if((sliceIndx<mBase && mSupports[i].getStart()==0)||(sliceIndx >= mSupports[i].getStart() + mBase && sliceIndx <= mSupports[i].getEnd() + mBase)){
sSimple = mSupports[i];
sSimple.setPoint(sSimple.getPoint()+QPoint(xOffset, yOffset));
sSimple.draw(pImage);
}
}
}
}
}
bool CrushedPrintJob::addImage(QImage* pImage){
CrushedBitMap CBM;
if(!CBM.crushSlice(pImage))return false;
// Update Extents
if(CBM.getExtents().left() < mJobExtents.left() ) mJobExtents.setLeft( CBM.getExtents().left());
if(CBM.getExtents().right() > mJobExtents.right() ) mJobExtents.setRight( CBM.getExtents().right());
if(CBM.getExtents().bottom() > mJobExtents.bottom()) mJobExtents.setBottom(CBM.getExtents().bottom());
if(CBM.getExtents().top() < mJobExtents.top() ) mJobExtents.setTop( CBM.getExtents().top());
// Update width and height
if(m_Width<CBM.getWidth())m_Width=CBM.getWidth();
if(m_Height<CBM.getHeight())m_Height=CBM.getHeight();
addCBM(CBM);
return true;
}
bool CrushedPrintJob::crushCurrentSlice(QImage* pImage)
{
return crushSlice(m_CurrentSlice, pImage);
}
bool CrushedPrintJob::crushSlice(int sliceIndx, QImage* pImage){
// Crushes the pImage and stores at m_CurrentSlice. Adjusts the job's width and height if needed
if(getCBMSlice(sliceIndx)==NULL)
return false;
bool bResult=getCBMSlice(sliceIndx)->crushSlice(pImage);
if(m_Width<getCBMSlice(sliceIndx)->getWidth())
m_Width=getCBMSlice(sliceIndx)->getWidth();
if(m_Height<getCBMSlice(sliceIndx)->getHeight())
m_Height=getCBMSlice(sliceIndx)->getHeight();
return bResult;
}
bool CrushedPrintJob::isWhitePixel(QPoint qPoint, int iSlice){
if(iSlice<0) iSlice = m_CurrentSlice;
if(iSlice<0 || iSlice> getTotalLayers()) return false;
CrushedBitMap* pCBM = getCBMSlice(iSlice);
return pCBM->isWhitePixel(qPoint);
}
void CrushedPrintJob::AddSupport(int iEndSlice, QPoint qCenter, int iSize, SupportType eType, int fastmode){
SimpleSupport support(qCenter, eType, iSize, 0-mBase, iEndSlice-mBase);
if(fastmode)
{
//Scan from iEndSlice down to 0, if we hit a white pixel set start to that layer
for(int i = iEndSlice; i>=0; i--)
{
if(isWhitePixel(qCenter,i)){
support.setStart(i-mBase);
break;
}
}
}
mSupports.append(support);
}
bool CrushedPrintJob::DeleteSupport(int iSlice, QPoint qCenter, int iRadius){
float dist;
float mindist = 1000.0;
int delindx;
iSlice = iSlice - mBase;
//Loop through all supports, if valid for this slice and share same qCenter, remove it
for(int i = 0; i<mSupports.size(); i++){
if(iSlice >= mSupports[i].getStart() && iSlice <= mSupports[i].getEnd() || iSlice -1 >= mSupports[i].getStart() && iSlice -1 <= mSupports[i].getEnd())
{
dist = qSqrt(qPow((qCenter.y() - mSupports[i].getPoint().y()),2)+qPow((qCenter.x() - mSupports[i].getPoint().x()),2));
if(dist < mindist)
{
mindist = dist;
delindx = i;
}
}
}
if(mindist<=iRadius)
{
mSupports.removeAt(delindx);
return true;
}
return false;
}