Kstars

linenode.cpp
1 /*
2  SPDX-FileCopyrightText: 2016 Artem Fedoskin <[email protected]>
3  SPDX-License-Identifier: GPL-2.0-or-later
4 */
5 
6 #include "linenode.h"
7 
8 #include "skymaplite.h"
9 #include "projections/projector.h"
10 #include "skycomponents/skiphashlist.h"
11 
12 #include <QLinkedList>
13 #include <QSGFlatColorMaterial>
14 
15 LineNode::LineNode(LineList *lineList, SkipHashList *skipList, QColor color, int width, Qt::PenStyle drawStyle)
16  : m_geometryNode(new QSGGeometryNode), m_lineList(lineList), m_skipList(skipList),
17  m_material(new QSGFlatColorMaterial), m_drawStyle(drawStyle)
18 {
19  m_geometryNode->setOpaqueMaterial(m_material);
20  //m_geometryNode->setFlag(QSGNode::OwnsMaterial);
21  m_material->setColor(color);
22 
24  m_geometryNode->setGeometry(m_geometry);
25  //m_geometryNode->setFlag(QSGNode::OwnsGeometry);
26  setWidth(width);
27 
28  appendChildNode(m_geometryNode);
29 }
30 
31 LineNode::~LineNode()
32 {
33  delete m_geometry;
34  delete m_material;
35 }
36 
37 void LineNode::setColor(QColor color)
38 {
39  if (m_material->color() != color)
40  {
41  m_material->setColor(color);
42  m_color = color;
43  m_geometryNode->markDirty(QSGNode::DirtyMaterial);
44  }
45 }
46 
47 void LineNode::setWidth(int width)
48 {
49  m_geometry->setLineWidth(width);
50  m_geometryNode->markDirty(QSGNode::DirtyGeometry);
51 }
52 
53 void LineNode::setDrawStyle(Qt::PenStyle style)
54 {
55  m_drawStyle = style;
56 }
57 
58 void LineNode::setStyle(QColor color, int width, Qt::PenStyle drawStyle)
59 {
60  setColor(color);
61  setWidth(width);
62  setDrawStyle(drawStyle);
63 }
64 
66 {
67  SkyList *points = m_lineList->points();
68 
69  m_geometry->setDrawingMode(GL_LINES);
70 
71  const Projector *m_proj = SkyMapLite::Instance()->projector();
72 
73  bool isVisible, isVisibleLast;
74 
75  QPointF oLast = m_proj->toScreen(points->first().get(), true, &isVisibleLast);
76 
77  // & with the result of checkVisibility to clip away things below horizon
78  isVisibleLast &= m_proj->checkVisibility(points->first().get());
79  QPointF oThis;
80 
81  QLinkedList<QPointF> newPoints;
82 
83  for (int j = 1; j < points->size(); j++)
84  {
85  SkyPoint *pThis = points->at(j).get();
86 
87  /*In regular KStars we first call checkVisibility and then toScreen
88  Here we minimize the number of calls to toScreen by proceeding only if
89  checkVisibility is true*/
90  oThis = m_proj->toScreen(pThis, true, &isVisible);
91  // & with the result of checkVisibility to clip away things below horizon
92  isVisible &= m_proj->checkVisibility(pThis);
93  bool doSkip = false;
94  if (m_skipList)
95  {
96  doSkip = m_skipList->skip(j);
97  }
98 
99  bool pointsVisible = false;
100  //Temporary solution to avoid random lines in Gnomonic projection and draw lines up to horizon
101  if (SkyMapLite::Instance()->projector()->type() == Projector::Gnomonic)
102  {
103  if (isVisible && isVisibleLast)
104  pointsVisible = true;
105  }
106  else
107  {
108  if (isVisible || isVisibleLast)
109  pointsVisible = true;
110  }
111 
112  if (!doSkip)
113  {
114  if (pointsVisible)
115  {
116  newPoints.append(oLast);
117  newPoints.append(oThis);
118  }
119  }
120  oLast = oThis;
121  isVisibleLast = isVisible;
122  }
123 
124  int size = newPoints.size();
125  m_geometry->allocate(size);
126 
127  QSGGeometry::Point2D *vertex = m_geometry->vertexDataAsPoint2D();
128 
130  int c = 0;
131 
132  while (i != newPoints.constEnd())
133  {
134  vertex[c].x = (*i).x();
135  vertex[c].y = (*i).y();
136  ++c;
137  ++i;
138  }
139  m_geometryNode->markDirty(QSGNode::DirtyGeometry);
140 }
void append(const T &value)
void setDrawingMode(unsigned int mode)
Stores dms coordinates for a point in the sky. for converting between coordinate systems.
Definition: skypoint.h:44
void updateGeometry()
Update lines based on the visibility of line segments in m_lineList.
Definition: linenode.cpp:65
void appendChildNode(QSGNode *node)
T & first()
void setGeometry(QSGGeometry *geometry)
void setOpaqueMaterial(QSGMaterial *material)
const QColor & color() const const
const QSGGeometry::AttributeSet & defaultAttributes_Point2D()
const Projector * projector() const
Get the current projector.
Definition: skymaplite.h:323
QLinkedList::const_iterator constEnd() const const
const T & at(int i) const const
QLinkedList::const_iterator constBegin() const const
void markDirty(QSGNode::DirtyState bits)
SkyList * points()
return the list of points for iterating or appending (or whatever).
Definition: linelist.h:33
void setColor(const QColor &color)
QPointF toScreen(const SkyPoint *o, bool oRefract=true, bool *onVisibleHemisphere=nullptr) const
This is exactly the same as toScreenVec but it returns a QPointF.
Definition: projector.cpp:93
QSGNode::NodeType type() const const
int size() const const
int size() const const
void allocate(int vertexCount, int indexCount)
LineNode(LineList *lineList, SkipHashList *skipList, QColor color, int width, Qt::PenStyle drawStyle)
Constructor.
Definition: linenode.cpp:15
PenStyle
QSGGeometry::Point2D * vertexDataAsPoint2D()
void setLineWidth(float width)
bool checkVisibility(const SkyPoint *p) const
Determine if the skypoint p is likely to be visible in the display window.
Definition: projector.cpp:183
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Mon Aug 15 2022 04:04:02 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.