/
Renderer.java
974 lines (845 loc) · 34.8 KB
/
Renderer.java
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
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
/* Copyright (C) 2008-2009 Gilleain Torrance <gilleain.torrance@gmail.com>
* 2008-2009 Arvid Berg <goglepox@users.sf.net>
* 2009 Stefan Kuhn <shk3@users.sf.net>
* 2009 Egon Willighagen <egonw@users.sf.net>
*
* Contact: cdk-devel@list.sourceforge.net
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*
* This program 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package org.openscience.cdk.renderer;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.List;
import javax.vecmath.Point2d;
import org.openscience.cdk.geometry.GeometryTools;
import org.openscience.cdk.interfaces.IAtomContainer;
import org.openscience.cdk.interfaces.IChemModel;
import org.openscience.cdk.interfaces.IMolecule;
import org.openscience.cdk.interfaces.IMoleculeSet;
import org.openscience.cdk.interfaces.IReaction;
import org.openscience.cdk.interfaces.IReactionSet;
import org.openscience.cdk.renderer.elements.ElementGroup;
import org.openscience.cdk.renderer.elements.IRenderingElement;
import org.openscience.cdk.renderer.font.IFontManager;
import org.openscience.cdk.renderer.generators.IGenerator;
import org.openscience.cdk.renderer.generators.BasicBondGenerator.BondLength;
import org.openscience.cdk.renderer.generators.BasicSceneGenerator.FitToScreen;
import org.openscience.cdk.renderer.generators.BasicSceneGenerator.FontName;
import org.openscience.cdk.renderer.generators.BasicSceneGenerator.Margin;
import org.openscience.cdk.renderer.generators.BasicSceneGenerator.Scale;
import org.openscience.cdk.renderer.generators.BasicSceneGenerator.UsedFontStyle;
import org.openscience.cdk.renderer.generators.BasicSceneGenerator.ZoomFactor;
import org.openscience.cdk.renderer.visitor.IDrawVisitor;
/**
* A general renderer for {@link IChemModel}s, {@link IReaction}s, and
* {@link IMolecule}s. The chem object
* is converted into a 'diagram' made up of {@link IRenderingElement}s. It takes
* an {@link IDrawVisitor} to do the drawing of the generated diagram. Various
* display properties can be set using the {@link RendererModel}.<p>
*
* This class has several usage patterns. For just painting fit-to-screen do:
* <pre>
* renderer.paintMolecule(molecule, visitor, drawArea)
* </pre>
* for painting at a scale determined by the bond length in the RendererModel:
* <pre>
* if (moleculeIsNew) {
* renderer.setup(molecule, drawArea);
* }
* Rectangle diagramSize = renderer.paintMolecule(molecule, visitor);
* // ...update scroll bars here
* </pre>
* to paint at full screen size, but not resize with each change:
* <pre>
* if (moleculeIsNew) {
* renderer.setScale(molecule);
* Rectangle diagramBounds = renderer.calculateDiagramBounds(molecule);
* renderer.setZoomToFit(diagramBounds, drawArea);
* renderer.paintMolecule(molecule, visitor);
* } else {
* Rectangle diagramSize = renderer.paintMolecule(molecule, visitor);
* // ...update scroll bars here
* }
* </pre>
* finally, if you are scrolling, and have not changed the diagram:
* <pre>
* renderer.repaint(visitor)
* </pre>
* will just repaint the previously generated diagram, at the same scale.<p>
*
* There are two sets of methods for painting IChemObjects - those that take
* a Rectangle that represents the desired draw area, and those that return a
* Rectangle that represents the actual draw area. The first are intended for
* drawing molecules fitted to the screen (where 'screen' means any drawing
* area) while the second type of method are for drawing bonds at the length
* defined by the {@link RendererModel} parameter bondLength.<p>
*
* There are two numbers used to transform the model so that it fits on screen.
* The first is <tt>scale</tt>, which is used to map model coordinates to
* screen coordinates. The second is <tt>zoom</tt> which is used to, well,
* zoom the on screen coordinates. If the diagram is fit-to-screen, then the
* ratio of the bounds when drawn using bondLength and the bounds of
* the screen is used as the zoom.<p>
*
* So, if the bond length on screen is set to 40, and the average bond length
* of the model is 2 (unitless, but roughly Ångstrom scale) then the
* scale will be 20. If the model is 10 units wide, then the diagram drawn at
* 100% zoom will be 10 * 20 = 200 in width on screen. If the screen is 400
* pixels wide, then fitting it to the screen will make the zoom 200%. Since the
* zoom is just a floating point number, 100% = 1 and 200% = 2.
*
* @author maclean
* @cdk.module renderextra
*/
public class Renderer extends AtomContainerRenderer implements IRenderer {
/**
* Generators specific to reactions
*/
private List<IGenerator<IReaction>> reactionGenerators;
/**
* A renderer that generates diagrams using the specified
* generators and manages fonts with the supplied font manager.
*
* @param generators
* a list of classes that implement the IGenerator interface
* @param fontManager
* a class that manages mappings between zoom and font sizes
*/
public Renderer(List<IGenerator<IAtomContainer>> generators, IFontManager fontManager) {
super(generators, fontManager);
}
public Renderer(List<IGenerator<IAtomContainer>> generators,
List<IGenerator<IReaction>> reactionGenerators,
IFontManager fontManager) {
this(generators, fontManager);
for (IGenerator<IReaction> generator : reactionGenerators)
rendererModel.registerParameters(generator);
this.reactionGenerators = reactionGenerators;
this.setup();
}
/**
* Setup the transformations necessary to draw this Chem Model.
*
* @param chemModel
* @param screen
*/
public void setup(IChemModel chemModel, Rectangle screen) {
this.setScale(chemModel);
Rectangle2D bounds = Renderer.calculateBounds(chemModel);
if(bounds != null)
this.modelCenter = new Point2d(bounds.getCenterX(), bounds.getCenterY());
this.drawCenter = new Point2d(screen.getCenterX(), screen.getCenterY());
this.setup();
}
/**
* Setup the transformations necessary to draw this Reaction Set.
*
* @param reactionSet
* @param screen
*/
public void setup(IReactionSet reactionSet, Rectangle screen) {
this.setScale(reactionSet);
Rectangle2D bounds = Renderer.calculateBounds(reactionSet);
this.modelCenter = new Point2d(bounds.getCenterX(), bounds.getCenterY());
this.drawCenter = new Point2d(screen.getCenterX(), screen.getCenterY());
this.setup();
}
/**
* Setup the transformations necessary to draw this Reaction.
*
* @param reaction
* @param screen
*/
public void setup(IReaction reaction, Rectangle screen) {
this.setScale(reaction);
Rectangle2D bounds = Renderer.calculateBounds(reaction);
this.modelCenter = new Point2d(bounds.getCenterX(), bounds.getCenterY());
this.drawCenter = new Point2d(screen.getCenterX(), screen.getCenterY());
this.setup();
}
/**
* Determine the overlap of the diagram with the screen, and shift (if
* necessary) the diagram draw center. It returns a rectangle only because
* that is a convenient class to hold the four parameters calculated, but it
* is not a rectangle representing an area...
*
* @param screenBounds
* the bounds of the screen
* @param diagramBounds
* the bounds of the diagram
* @return the shape that the screen should be
*/
public Rectangle shift(Rectangle screenBounds, Rectangle diagramBounds) {
int screenMaxX = screenBounds.x + screenBounds.width;
int screenMaxY = screenBounds.y + screenBounds.height;
int diagramMaxX = diagramBounds.x + diagramBounds.width;
int diagramMaxY = diagramBounds.y + diagramBounds.height;
int leftOverlap = screenBounds.x - diagramBounds.x;
int rightOverlap = diagramMaxX - screenMaxX;
int topOverlap = screenBounds.y - diagramBounds.y;
int bottomOverlap = diagramMaxY - screenMaxY;
int dx = 0;
int dy = 0;
int w = screenBounds.width;
int h = screenBounds.height;
if (leftOverlap > 0) {
dx = leftOverlap;
}
if (rightOverlap > 0) {
w += rightOverlap;
}
if (topOverlap > 0) {
dy = topOverlap;
}
if (bottomOverlap > 0) {
h += bottomOverlap;
}
if (dx != 0 || dy != 0) {
this.shiftDrawCenter(dx, dy);
}
return new Rectangle(dx, dy, w, h);
}
/**
* Set the scale for an IChemModel. It calculates the average bond length of
* the model and calculates the multiplication factor to transform this
* to the bond length that is set in the RendererModel.
*
* @param chemModel
*/
public void setScale(IChemModel chemModel) {
double bondLength = Renderer.calculateAverageBondLength(chemModel);
double scale = this.calculateScaleForBondLength(bondLength);
// store the scale so that other components can access it
this.rendererModel.getParameter(Scale.class).setValue(scale);
}
/**
* Set the scale for an IReactionSet. It calculates the average bond length
* of the model and calculates the multiplication factor to transform this
* to the bond length that is set in the RendererModel.
*
* @param reactionSet
*/
public void setScale(IReactionSet reactionSet) {
double bondLength = Renderer.calculateAverageBondLength(reactionSet);
double scale = this.calculateScaleForBondLength(bondLength);
// store the scale so that other components can access it
this.rendererModel.getParameter(Scale.class).setValue(scale);
}
/**
* Set the scale for an IReaction. It calculates the average bond length
* of the model and calculates the multiplication factor to transform this
* to the bond length that is set in the RendererModel.
* @param reaction
*/
public void setScale(IReaction reaction) {
double bondLength = Renderer.calculateAverageBondLength(reaction);
double scale = this.calculateScaleForBondLength(bondLength);
// store the scale so that other components can access it
this.rendererModel.getParameter(Scale.class).setValue(scale);
}
/**
* Set the scale for an IMoleculeSet. It calculates the average bond length
* of the model and calculates the multiplication factor to transform this
* to the bond length that is set in the RendererModel.
* @param moleculeSet
*/
public void setScale(IMoleculeSet moleculeSet) {
double bondLength = Renderer.calculateAverageBondLength(moleculeSet);
double scale = this.calculateScaleForBondLength(bondLength);
// store the scale so that other components can access it
this.rendererModel.getParameter(Scale.class).setValue(scale);
}
/**
* Paint an IChemModel using the IDrawVisitor at a scale determined by the
* bond length in RendererModel.
*
* @param chemModel
* the chem model to draw
* @param drawVisitor
* the visitor used to draw with
* @return the rectangular area that the diagram will occupy on screen
*/
public Rectangle paintChemModel(
IChemModel chemModel, IDrawVisitor drawVisitor) {
IMoleculeSet moleculeSet = chemModel.getMoleculeSet();
IReactionSet reactionSet = chemModel.getReactionSet();
if (moleculeSet == null && reactionSet != null) {
return paintReactionSet(reactionSet, drawVisitor);
}
if (moleculeSet != null && reactionSet == null) {
return paintMoleculeSet(moleculeSet, drawVisitor);
}
if (moleculeSet != null && reactionSet != null) {
Rectangle2D totalBounds = Renderer.calculateBounds(reactionSet);
totalBounds = totalBounds.createUnion(
Renderer.calculateBounds(moleculeSet));
this.setupTransformNatural(totalBounds);
ElementGroup diagram = new ElementGroup();
for (IReaction reaction : reactionSet.reactions()) {
diagram.add(this.generateDiagram(reaction));
}
diagram.add(this.generateDiagram(moleculeSet));
this.paint(drawVisitor, diagram);
// the size of the painted diagram is returned
return this.convertToDiagramBounds(totalBounds);
}
return new Rectangle(0, 0, 0, 0);
}
public Rectangle paintReactionSet(
IReactionSet reactionSet, IDrawVisitor drawVisitor) {
// total up the bounding boxes
Rectangle2D totalBounds = new Rectangle2D.Double();
for (IReaction reaction : reactionSet.reactions()) {
Rectangle2D modelBounds = Renderer.calculateBounds(reaction);
if (totalBounds == null) {
totalBounds = modelBounds;
} else {
totalBounds = totalBounds.createUnion(modelBounds);
}
}
// setup and draw
this.setupTransformNatural(totalBounds);
ElementGroup diagram = new ElementGroup();
for (IReaction reaction : reactionSet.reactions()) {
diagram.add(this.generateDiagram(reaction));
}
this.paint(drawVisitor, diagram);
// the size of the painted diagram is returned
return this.convertToDiagramBounds(totalBounds);
}
public Rectangle paintReaction(
IReaction reaction, IDrawVisitor drawVisitor) {
// calculate the bounds
Rectangle2D modelBounds = Renderer.calculateBounds(reaction);
// setup and draw
this.setupTransformNatural(modelBounds);
IRenderingElement diagram = this.generateDiagram(reaction);
this.paint(drawVisitor, diagram);
return this.convertToDiagramBounds(modelBounds);
}
public Rectangle paintMoleculeSet(
IMoleculeSet moleculeSet, IDrawVisitor drawVisitor) {
// total up the bounding boxes
Rectangle2D totalBounds = new Rectangle2D.Double();
for (IAtomContainer molecule : moleculeSet.molecules()) {
Rectangle2D modelBounds = Renderer.calculateBounds(molecule);
if (totalBounds == null) {
totalBounds = modelBounds;
} else {
totalBounds = totalBounds.createUnion(modelBounds);
}
}
// setup and draw
this.setupTransformNatural(totalBounds);
ElementGroup diagram = new ElementGroup();
for (IAtomContainer molecule : moleculeSet.molecules()) {
diagram.add(this.generateDiagram(molecule));
}
this.paint(drawVisitor, diagram);
return this.convertToDiagramBounds(totalBounds);
}
/**
* Paint a ChemModel.
*
* @param chemModel
* @param drawVisitor the visitor that does the drawing
* @param bounds the bounds of the area to paint on.
* @param resetCenter
* if true, set the modelCenter to the center of the ChemModel's bounds.
*/
public void paintChemModel(IChemModel chemModel,
IDrawVisitor drawVisitor, Rectangle2D bounds, boolean resetCenter) {
// check for an empty model
IMoleculeSet moleculeSet = chemModel.getMoleculeSet();
IReactionSet reactionSet = chemModel.getReactionSet();
// nasty, but it seems that reactions can be read in as ChemModels
// with BOTH a ReactionSet AND a MoleculeSet...
if (moleculeSet == null || reactionSet != null) {
if (reactionSet != null) {
paintReactionSet(reactionSet, drawVisitor, bounds, resetCenter);
}
return;
}
// calculate the total bounding box
Rectangle2D modelBounds = Renderer.calculateBounds(moleculeSet);
this.setupTransformToFit(bounds, modelBounds,
Renderer.calculateAverageBondLength(chemModel), resetCenter);
// generate the elements
IRenderingElement diagram = this.generateDiagram(moleculeSet);
// paint it
this.paint(drawVisitor, diagram);
}
/**
* Paint a set of reactions.
*
* @param reaction the reaction to paint
* @param drawVisitor the visitor that does the drawing
* @param bounds the bounds on the screen
* @param resetCenter
* if true, set the draw center to be the center of bounds
*/
public void paintReactionSet(IReactionSet reactionSet,
IDrawVisitor drawVisitor, Rectangle2D bounds, boolean resetCenter) {
// total up the bounding boxes
Rectangle2D totalBounds = null;
for (IReaction reaction : reactionSet.reactions()) {
Rectangle2D modelBounds = Renderer.calculateBounds(reaction);
if (totalBounds == null) {
totalBounds = modelBounds;
} else {
totalBounds = totalBounds.createUnion(modelBounds);
}
}
this.setupTransformToFit(bounds, totalBounds,
Renderer.calculateAverageBondLength(reactionSet), resetCenter);
ElementGroup diagram = new ElementGroup();
for (IReaction reaction : reactionSet.reactions()) {
diagram.add(this.generateDiagram(reaction));
}
// paint them all
this.paint(drawVisitor, diagram);
}
/**
* Paint a reaction.
*
* @param reaction the reaction to paint
* @param drawVisitor the visitor that does the drawing
* @param bounds the bounds on the screen
* @param resetCenter
* if true, set the draw center to be the center of bounds
*/
public void paintReaction(IReaction reaction, IDrawVisitor drawVisitor,
Rectangle2D bounds, boolean resetCenter) {
// calculate the bounds
Rectangle2D modelBounds = Renderer.calculateBounds(reaction);
this.setupTransformToFit(bounds, modelBounds,
Renderer.calculateAverageBondLength(reaction), resetCenter);
// generate the elements
IRenderingElement diagram = this.generateDiagram(reaction);
// paint it
this.paint(drawVisitor, diagram);
}
/**
* Paint a set of molecules.
*
* @param reaction the reaction to paint
* @param drawVisitor the visitor that does the drawing
* @param bounds the bounds on the screen
* @param resetCenter
* if true, set the draw center to be the center of bounds
*/
public void paintMoleculeSet(IMoleculeSet molecules,
IDrawVisitor drawVisitor, Rectangle2D bounds, boolean resetCenter) {
// total up the bounding boxes
Rectangle2D totalBounds = null;
for (IAtomContainer molecule : molecules.molecules()) {
Rectangle2D modelBounds = Renderer.calculateBounds(molecule);
if (totalBounds == null) {
totalBounds = modelBounds;
} else {
totalBounds = totalBounds.createUnion(modelBounds);
}
}
this.setupTransformToFit(bounds, totalBounds,
Renderer.calculateAverageBondLength(molecules), resetCenter);
ElementGroup diagram = new ElementGroup();
for (IAtomContainer molecule : molecules.molecules()) {
diagram.add(this.generateDiagram(molecule));
}
this.paint(drawVisitor, diagram);
}
/**
* Repaint using the cached diagram
*
* @param drawVisitor the wrapper for the graphics object that draws
*/
public void repaint(IDrawVisitor drawVisitor) {
this.paint(drawVisitor, cachedDiagram);
}
/**
* Given a chem model, calculates the bounding rectangle in screen space.
*
* @param model the model to draw.
* @return a rectangle in screen space.
*/
public Rectangle calculateDiagramBounds(IChemModel model) {
IMoleculeSet moleculeSet = model.getMoleculeSet();
IReactionSet reactionSet = model.getReactionSet();
if ((moleculeSet == null && reactionSet == null)) {
return new Rectangle();
}
Rectangle2D moleculeBounds = null;
Rectangle2D reactionBounds = null;
if (moleculeSet != null) {
moleculeBounds = Renderer.calculateBounds(moleculeSet);
}
if (reactionSet != null) {
reactionBounds = Renderer.calculateBounds(reactionSet);
}
if (moleculeBounds == null) {
return this.calculateScreenBounds(reactionBounds);
} else if (reactionBounds == null) {
return this.calculateScreenBounds(moleculeBounds);
} else {
Rectangle2D allbounds = new Rectangle2D.Double();
Rectangle2D.union(moleculeBounds, reactionBounds, allbounds);
return this.calculateScreenBounds(allbounds);
}
}
public Rectangle calculateDiagramBounds(IReactionSet reactionSet) {
return this.calculateScreenBounds(
Renderer.calculateBounds(reactionSet));
}
public Rectangle calculateDiagramBounds(IReaction reaction) {
return this.calculateScreenBounds(
Renderer.calculateBounds(reaction));
}
public Rectangle calculateDiagramBounds(IMoleculeSet moleculeSet) {
return this.calculateScreenBounds(
Renderer.calculateBounds(moleculeSet));
}
public static Rectangle2D calculateBounds(IChemModel chemModel) {
IMoleculeSet moleculeSet = chemModel.getMoleculeSet();
IReactionSet reactionSet = chemModel.getReactionSet();
Rectangle2D totalBounds = null;
if (moleculeSet != null) {
totalBounds = Renderer.calculateBounds(moleculeSet);
}
if (reactionSet != null) {
if (totalBounds == null) {
totalBounds = Renderer.calculateBounds(reactionSet);
} else {
totalBounds = totalBounds.createUnion(
Renderer.calculateBounds(reactionSet));
}
}
return totalBounds;
}
public static Rectangle2D calculateBounds(IReactionSet reactionSet) {
Rectangle2D totalBounds = new Rectangle2D.Double();
for (IReaction reaction : reactionSet.reactions()) {
Rectangle2D reactionBounds = Renderer.calculateBounds(reaction);
if (totalBounds.isEmpty()) {
totalBounds = reactionBounds;
} else {
Rectangle2D.union(totalBounds, reactionBounds, totalBounds);
}
}
return totalBounds;
}
public static Rectangle2D calculateBounds(IReaction reaction) {
// get the participants in the reaction
IMoleculeSet reactants = reaction.getReactants();
IMoleculeSet products = reaction.getProducts();
if (reactants == null || products == null) return null;
// determine the bounds of everything in the reaction
Rectangle2D reactantsBounds = Renderer.calculateBounds(reactants);
return reactantsBounds.createUnion(Renderer.calculateBounds(products));
}
public static Rectangle2D calculateBounds(IMoleculeSet moleculeSet) {
Rectangle2D totalBounds = new Rectangle2D.Double();
for (int i = 0; i < moleculeSet.getAtomContainerCount(); i++) {
IAtomContainer ac = moleculeSet.getAtomContainer(i);
Rectangle2D acBounds = Renderer.calculateBounds(ac);
if (totalBounds.isEmpty()) {
totalBounds = acBounds;
} else {
Rectangle2D.union(totalBounds, acBounds, totalBounds);
}
}
return totalBounds;
}
/**
*
*
* @param model the model for which to calculate the average bond length
*/
public static double calculateAverageBondLength(IChemModel model) {
// empty models have to have a scale
IMoleculeSet moleculeSet = model.getMoleculeSet();
if (moleculeSet == null) {
IReactionSet reactionSet = model.getReactionSet();
if (reactionSet != null) {
return Renderer.calculateAverageBondLength(reactionSet);
}
return 0.0;
}
return Renderer.calculateAverageBondLength(moleculeSet);
}
public static double calculateAverageBondLength(IReactionSet reactionSet) {
double averageBondModelLength = 0.0;
for (IReaction reaction : reactionSet.reactions()) {
averageBondModelLength +=
Renderer.calculateAverageBondLength(reaction);
}
return averageBondModelLength / reactionSet.getReactionCount();
}
public static double calculateAverageBondLength(IReaction reaction) {
IMoleculeSet reactants = reaction.getReactants();
double reactantAverage = 0.0;
if (reactants != null) {
reactantAverage =
Renderer.calculateAverageBondLength(reactants) /
reactants.getAtomContainerCount();
}
IMoleculeSet products = reaction.getProducts();
double productAverage = 0.0;
if (products != null) {
productAverage =
Renderer.calculateAverageBondLength(products) /
products.getAtomContainerCount();
}
if (productAverage == 0.0 && reactantAverage == 0.0) {
return 1.0;
} else {
return (productAverage + reactantAverage) / 2.0;
}
}
public static double calculateAverageBondLength(IMoleculeSet moleculeSet) {
double averageBondModelLength = 0.0;
for (IAtomContainer atomContainer : moleculeSet.molecules()) {
averageBondModelLength +=
GeometryTools.getBondLengthAverage(atomContainer);
}
return averageBondModelLength / moleculeSet.getAtomContainerCount();
}
public RendererModel getRenderer2DModel() {
return this.rendererModel;
}
public void setModelCenter(double x, double y) {
this.modelCenter = new Point2d(x, y);
setup();
}
public void setDrawCenter(double x, double y) {
this.drawCenter = new Point2d(x, y);
setup();
}
public void setZoom(double z) {
getRenderer2DModel().getParameter(
ZoomFactor.class).setValue( z );
setup();
}
/**
* Move the draw center by dx and dy.
*
* @param dx
* the x shift
* @param dy
* the y shift
*/
public void shiftDrawCenter(double dx, double dy) {
this.drawCenter.set(this.drawCenter.x + dx, this.drawCenter.y + dy);
setup();
}
public Point2d getDrawCenter() {
return drawCenter;
}
public Point2d getModelCenter() {
return modelCenter;
}
/**
* Calculate and set the zoom factor needed to completely fit the diagram
* onto the screen bounds.
*
* @param diagramBounds
* @param drawBounds
*/
public void setZoomToFit(double drawWidth,
double drawHeight,
double diagramWidth,
double diagramHeight) {
double m = this.rendererModel
.getParameter(Margin.class).getValue();
// determine the zoom needed to fit the diagram to the screen
double widthRatio = drawWidth / (diagramWidth + (2 * m));
double heightRatio = drawHeight / (diagramHeight + (2 * m));
double zoom = Math.min(widthRatio, heightRatio);
this.fontManager.setFontForZoom(zoom);
// record the zoom in the model, so that generators can use it
this.rendererModel.getParameter(
ZoomFactor.class).setValue(zoom);
}
/**
* The target method for paintChemModel, paintReaction, and paintMolecule.
*
* @param drawVisitor
* the visitor to draw with
* @param diagram
* the IRenderingElement tree to render
*/
private void paint(IDrawVisitor drawVisitor,
IRenderingElement diagram) {
if (diagram == null) return;
// cache the diagram for quick-redraw
this.cachedDiagram = diagram;
this.fontManager.setFontName(
this.rendererModel.getParameter(FontName.class).getValue()
);
this.fontManager.setFontStyle(
this.rendererModel.getParameter(UsedFontStyle.class)
.getValue()
);
drawVisitor.setFontManager(this.fontManager);
drawVisitor.setTransform(this.transform);
drawVisitor.setRendererModel(this.rendererModel);
diagram.accept(drawVisitor);
}
/**
* Set the transform for a non-fit to screen paint.
*
* @param modelBounds
* the bounding box of the model
*/
private void setupTransformNatural(Rectangle2D modelBounds) {
double zoom = this.rendererModel.getParameter(
ZoomFactor.class).getValue();
this.fontManager.setFontForZoom(zoom);
this.setup();
}
/**
* Sets the transformation needed to draw the model on the canvas when
* the diagram needs to fit the screen.
*
* @param screenBounds
* the bounding box of the draw area
* @param modelBounds
* the bounding box of the model
* @param bondLength
* the average bond length of the model
* @param reset
* if true, model center will be set to the modelBounds center
* and the scale will be re-calculated
*/
private void setupTransformToFit(Rectangle2D screenBounds,
Rectangle2D modelBounds,
double bondLength,
boolean reset) {
if (screenBounds == null) return;
this.setDrawCenter(
screenBounds.getCenterX(), screenBounds.getCenterY());
double scale = this.calculateScaleForBondLength(bondLength);
double drawWidth = screenBounds.getWidth();
double drawHeight = screenBounds.getHeight();
double diagramWidth = modelBounds.getWidth() * scale;
double diagramHeight = modelBounds.getHeight() * scale;
this.setZoomToFit(drawWidth, drawHeight, diagramWidth, diagramHeight);
// this controls whether editing a molecule causes it to re-center
// with each change or not
if (reset || rendererModel.getParameter(FitToScreen.class).getValue()) {
this.setModelCenter(
modelBounds.getCenterX(), modelBounds.getCenterY());
}
// set the scale in the renderer model for the generators
this.rendererModel.getParameter(Scale.class).setValue(scale);
this.setup();
}
/**
* Given a bond length for a model, calculate the scale that will transform
* this length to the on screen bond length in RendererModel.
*
* @param modelBondLength
* @param reset
* @return
*/
private double calculateScaleForBondLength(double modelBondLength) {
if (Double.isNaN(modelBondLength) || modelBondLength == 0) {
return rendererModel.getParameter(Scale.class).getDefault();
} else {
return this.rendererModel.getParameter(BondLength.class)
.getValue() / modelBondLength;
}
}
/**
* Calculate the bounds of the diagram on screen, given the current scale,
* zoom, and margin.
*
* @param modelBounds
* the bounds in model space of the chem object
* @return the bounds in screen space of the drawn diagram
*/
private Rectangle convertToDiagramBounds(Rectangle2D modelBounds) {
double cx = modelBounds.getCenterX();
double cy = modelBounds.getCenterY();
double mw = modelBounds.getWidth();
double mh = modelBounds.getHeight();
double scale = rendererModel.getParameter(Scale.class).getValue();
double zoom = rendererModel.getParameter(ZoomFactor.class).getValue();
Point2d mc = this.toScreenCoordinates(cx, cy);
// special case for 0 or 1 atoms
if (mw == 0 && mh == 0) {
return new Rectangle((int)mc.x, (int)mc.y, 0, 0);
}
double margin = this.rendererModel
.getParameter(Margin.class).getValue();
int w = (int) ((scale * zoom * mw) + (2 * margin));
int h = (int) ((scale * zoom * mh) + (2 * margin));
int x = (int) (mc.x - w / 2);
int y = (int) (mc.y - h / 2);
return new Rectangle(x, y, w, h);
}
private void setup() {
double scale = rendererModel.getParameter(Scale.class).getValue();
double zoom = rendererModel.getParameter(ZoomFactor.class).getValue();
// set the transform
try {
this.transform = new AffineTransform();
this.transform.translate(this.drawCenter.x, this.drawCenter.y);
this.transform.scale(1,-1); // Converts between CDK Y-up & Java2D Y-down coordinate-systems
this.transform.scale(scale, scale);
this.transform.scale(zoom, zoom);
this.transform.translate(-this.modelCenter.x, -this.modelCenter.y);
// System.err.println(String.format(
// "drawCenter=%s scale=%s zoom=%s modelCenter=%s",
// this.drawCenter,
// this.scale,
// this.zoom,
// this.modelCenter));
} catch (NullPointerException npe) {
// one of the drawCenter or modelCenter points have not been set!
System.err.println(String.format(
"null pointer when setting transform: " +
"drawCenter=%s scale=%s zoom=%s modelCenter=%s",
this.drawCenter,
scale,
zoom,
this.modelCenter));
}
}
private IRenderingElement generateDiagram(IReaction reaction) {
ElementGroup diagram = new ElementGroup();
for (IGenerator<IReaction> generator : this.reactionGenerators) {
diagram.add(generator.generate(reaction, rendererModel));
}
diagram.add(generateDiagram(reaction.getReactants()));
diagram.add(generateDiagram(reaction.getProducts()));
return diagram;
}
private IRenderingElement generateDiagram(IMoleculeSet moleculeSet) {
ElementGroup diagram = new ElementGroup();
for (int i = 0; i < moleculeSet.getAtomContainerCount(); i++) {
IAtomContainer ac = moleculeSet.getAtomContainer(i);
for (IGenerator<IAtomContainer> generator : this.generators) {
diagram.add(generator.generate(ac, this.rendererModel));
}
}
return diagram;
}
public List<IGenerator<IReaction>> getReactionGenerators(){
return new ArrayList<IGenerator<IReaction>>(reactionGenerators);
}
}