/** @brief Drag finger on screen, now release your finger. * @param p [in] Point where we release the finger. * @return true when correct. */ bool ChandWriter::onPaintingStop( const Cpoint &point) { if ( m_surface ==NULL) { return false; } //p =CdialogEvent::Instance()->lastMouse(); Cpoint p =point-(m_rect.leftTop()*8); addPoint( p.x, p.y); //zinnia_character_add( m_character, m_stroke, p.x, p.y); #ifdef USE_SDL2 m_graphics->setRenderArea( m_surface); m_graphics->line( m_lastPoint.x, m_lastPoint.y, p.x, p.y); m_graphics->setRenderArea( NULL); #else CtextSurface::line( m_surface, m_lastPoint, p); #endif m_started =false; m_stroke++; m_lastPoint =Cpoint(-100,-100); #ifdef USE_ZINNIA if ( Cgraphics::m_defaults.handwriting_detection_enabled) { if ( m_result) { zinnia_result_destroy( m_result); m_result =NULL; } m_result = zinnia_recognizer_classify( m_recognizer, m_character, 50); } #endif return true; }
/** @brief Clear the handwriter background. */ void ChandWriter::clearImage() { if ( Cgraphics::m_defaults.handwriting_detection_enabled) { #ifdef USE_SDL2 assert(0); #else SDL_Rect rect; rect.h =(Uint16)m_surface->h; rect.w =(Uint16)m_surface->w; rect.x =0; rect.y =0; SDL_FillRect( m_surface, &rect, m_background.getColour()); #endif m_stroke =0; m_index =0; m_lastPoint =Cpoint(-5,-100); #ifdef USE_ZINNIA if ( m_character) { zinnia_character_clear( m_character); zinnia_character_set_width( m_character, m_rect.width()*8); zinnia_character_set_height( m_character, m_rect.height()*8); } #endif } }
unsigned short int Cline::intersectionsWithCircle(Cpoint c, double r, vector<Cpoint> &intersections) { intersections.clear(); // move circle to (0,0) //Cpoint p1 = aa - c; cout << "DEBUG: p1 is " << p1 << endl; //Cpoint p2 = bb - c; cout << "DEBUG: p2 is " << p2 << endl; Cpoint p1(aa.get_xx() - c.get_xx(), aa.get_yy() - c.get_yy()); //cout << "DEBUG: p1 is " << p1 << endl; Cpoint p2(bb.get_xx() - c.get_xx(), bb.get_yy() - c.get_yy()); //cout << "DEBUG: p1 is " << p1 << endl; // discriminant double dx = p2.get_xx() - p1.get_xx(); double dy = p2.get_yy() - p1.get_yy(); double dr_sq = dx*dx + dy*dy; double D = p1.get_xx() * p2.get_yy() - p2.get_xx() * p1.get_yy(); double disc = r*r * dr_sq - D*D; if(disc < 0) return 0; else if(disc == 0) { double x = D * dy / dr_sq; double y = -D * dx / dr_sq; // check if point belongs to segment Cpoint i = Cpoint(x + c.get_xx(), y + c.get_yy()); if( i.d2point2(&aa) <= aa.d2point2(&bb) && i.d2point2(&bb) <= aa.d2point2(&bb)) intersections.push_back( i ); return intersections.size(); } else { int sign_dy = (dy >= 0) ? 1 : -1; double x1 = ( D * dy + sign_dy * dx * sqrt(disc) ) / dr_sq; double x2 = ( D * dy - sign_dy * dx * sqrt(disc) ) / dr_sq; double abs_dy = ( dy >= 0 ) ? dy : -dy; double y1 = ( -D * dx + abs_dy * sqrt(disc) ) / dr_sq; double y2 = ( -D * dx - abs_dy * sqrt(disc) ) / dr_sq; Cpoint i1 = Cpoint(x1 + c.get_xx(), y1 + c.get_yy()); Cpoint i2 = Cpoint(x2 + c.get_xx(), y2 + c.get_yy()); if( i1.d2point2(&aa) <= aa.d2point2(&bb) && i1.d2point2(&bb) <= aa.d2point2(&bb)) intersections.push_back( i1 ); if( i2.d2point2(&aa) <= aa.d2point2(&bb) && i2.d2point2(&bb) <= aa.d2point2(&bb)) intersections.push_back( i2 ); return intersections.size(); } }
// ----------------------------------------------------------------------------- int Cluster_DPeaks::Cluster_DiscreteDensity() { mprintf("\tStarting DPeaks clustering, discrete density calculation.\n"); Points_.clear(); // First determine which frames are being clustered. for (int frame = 0; frame < (int)FrameDistances_.Nframes(); ++frame) if (!FrameDistances_.IgnoringRow( frame )) Points_.push_back( Cpoint(frame) ); // Sanity check. if (Points_.size() < 2) { mprinterr("Error: Only 1 frame in initial clustering.\n"); return 1; } // For each point, determine how many others are within epsilon. Also // determine maximum distance between any two points. mprintf("\tDetermining local density of each point.\n"); ProgressBar cluster_progress( Points_.size() ); double maxDist = -1.0; for (Carray::iterator point0 = Points_.begin(); point0 != Points_.end(); ++point0) { cluster_progress.Update(point0 - Points_.begin()); int density = 0; for (Carray::const_iterator point1 = Points_.begin(); point1 != Points_.end(); ++point1) { if (point0 != point1) { double dist = FrameDistances_.GetFdist(point0->Fnum(), point1->Fnum()); maxDist = std::max(maxDist, dist); if ( dist < epsilon_ ) density++; } } point0->SetPointsWithinEps( density ); } mprintf("DBG: Max dist= %g\n", maxDist); // DEBUG: Frame/Density CpptrajFile fdout; fdout.OpenWrite("fd.dat"); for (Carray::const_iterator point = Points_.begin(); point != Points_.end(); ++point) fdout.Printf("%i %i\n", point->Fnum()+1, point->PointsWithinEps()); fdout.CloseFile(); // Sort by density here. Otherwise array indices will be invalid later. std::sort( Points_.begin(), Points_.end(), Cpoint::pointsWithinEps_sort() ); // For each point, find the closest point that has higher density. Since // array is now sorted by density the last point has the highest density. Points_.back().SetDist( maxDist ); mprintf("\tFinding closest neighbor point with higher density for each point.\n"); unsigned int lastidx = Points_.size() - 1; cluster_progress.SetupProgress( lastidx ); for (unsigned int idx0 = 0; idx0 != lastidx; idx0++) { cluster_progress.Update( idx0 ); double min_dist = maxDist; int nearestIdx = -1; // Index of nearest neighbor with higher density Cpoint& point0 = Points_[idx0]; //mprintf("\nDBG:\tSearching for nearest neighbor to idx %u with higher density than %i.\n", // idx0, point0.PointsWithinEps()); // Since array is sorted by density we can start at the next point. for (unsigned int idx1 = idx0+1; idx1 != Points_.size(); idx1++) { Cpoint const& point1 = Points_[idx1]; double dist1_2 = FrameDistances_.GetFdist(point0.Fnum(), point1.Fnum()); if (point1.PointsWithinEps() > point0.PointsWithinEps()) { if (dist1_2 < min_dist) { min_dist = dist1_2; nearestIdx = (int)idx1; //mprintf("DBG:\t\tNeighbor idx %i is closer (density %i, distance %g)\n", // nearestIdx, point1.PointsWithinEps(), min_dist); } } } point0.SetDist( min_dist ); //mprintf("DBG:\tClosest point to %u with higher density is %i (distance %g)\n", // idx0, nearestIdx, min_dist); point0.SetNearestIdx( nearestIdx ); } // Plot density vs distance for each point. if (!dvdfile_.empty()) { CpptrajFile output; if (output.OpenWrite(dvdfile_)) mprinterr("Error: Could not open density vs distance plot '%s' for write.\n", dvdfile_.c_str()); // TODO: Make fatal? else { output.Printf("%-10s %10s %s %10s %10s\n", "#Density", "Distance", "Frame", "Idx", "Neighbor"); for (Carray::const_iterator point = Points_.begin(); point != Points_.end(); ++point) output.Printf("%-10i %10g \"%i\" %10u %10i\n", point->PointsWithinEps(), point->Dist(), point->Fnum()+1, point-Points_.begin(), point->NearestIdx()); output.CloseFile(); } } return 0; }
// ----------------------------------------------------------------------------- int Cluster_DPeaks::Cluster_GaussianKernel() { mprintf("\tStarting DPeaks clustering. Using Gaussian kernel to calculate density.\n"); // First determine which frames are being clustered. Points_.clear(); int oidx = 0; for (int frame = 0; frame < (int)FrameDistances_.Nframes(); ++frame) if (!FrameDistances_.IgnoringRow( frame )) Points_.push_back( Cpoint(frame, oidx++) ); // Sanity check. if (Points_.size() < 2) { mprinterr("Error: Only 1 frame in initial clustering.\n"); return 1; } // Sort distances std::vector<float> Distances; for (ClusterMatrix::const_iterator mat = FrameDistances_.begin(); mat != FrameDistances_.end(); ++mat) Distances.push_back( *mat ); std::sort( Distances.begin(), Distances.end() ); unsigned int idx = (unsigned int)((double)Distances.size() * 0.02); double bandwidth = (double)Distances[idx]; mprintf("idx= %u, bandwidth= %g\n", idx, bandwidth); // Density via Gaussian kernel double maxDist = -1.0; for (unsigned int i = 0; i != Points_.size(); i++) { for (unsigned int j = i+1; j != Points_.size(); j++) { double dist = FrameDistances_.GetFdist(Points_[i].Fnum(), Points_[j].Fnum()); maxDist = std::max( maxDist, dist ); dist /= bandwidth; double gk = exp(-(dist *dist)); Points_[i].AddDensity( gk ); Points_[j].AddDensity( gk ); } } mprintf("Max dist= %g\n", maxDist); CpptrajFile rhoOut; rhoOut.OpenWrite("rho.dat"); for (unsigned int i = 0; i != Points_.size(); i++) rhoOut.Printf("%u %g\n", i+1, Points_[i].Density()); rhoOut.CloseFile(); // Sort by density, descending std::stable_sort( Points_.begin(), Points_.end(), Cpoint::density_sort_descend() ); CpptrajFile ordrhoOut; ordrhoOut.OpenWrite("ordrho.dat"); for (unsigned int i = 0; i != Points_.size(); i++) ordrhoOut.Printf("%u %g %i %i\n", i+1, Points_[i].Density(), Points_[i].Fnum()+1, Points_[i].Oidx()+1); ordrhoOut.CloseFile(); // Determine minimum distances int first_idx = Points_[0].Oidx(); Points_[first_idx].SetDist( -1.0 ); Points_[first_idx].SetNearestIdx(-1); for (unsigned int ii = 1; ii != Points_.size(); ii++) { int ord_i = Points_[ii].Oidx(); Points_[ord_i].SetDist( maxDist ); for (unsigned int jj = 0; jj != ii; jj++) { int ord_j = Points_[jj].Oidx(); double dist = FrameDistances_.GetFdist(Points_[ord_i].Fnum(), Points_[ord_j].Fnum()); if (dist < Points_[ord_i].Dist()) { Points_[ord_i].SetDist( dist ); Points_[ord_j].SetNearestIdx( ord_j ); } } } if (!dvdfile_.empty()) { CpptrajFile output; if (output.OpenWrite(dvdfile_)) return 1; for (Carray::const_iterator point = Points_.begin(); point != Points_.end(); ++point) output.Printf("%g %g %i\n", point->Density(), point->Dist(), point->NearestIdx()+1); output.CloseFile(); } return 0; }
/** Gets vv vector normalized */ Cpoint Cline::getDirection() const { return Cpoint(vv.get_xx()/length, vv.get_yy()/length); }