📄 node.cc
字号:
/* * Copyright (c) 1991,1993 Regents of the University of California. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the Computer Systems * Engineering Group at Lawrence Berkeley Laboratory. * 4. Neither the name of the University nor of the Laboratory may be used * to endorse or promote products derived from this software without * specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * @(#) $Header: /cvsroot/nsnam/nam-1/node.cc,v 1.62 2001/08/31 03:18:15 mehringe Exp $ (LBL) */#include <stdlib.h>#ifdef WIN32#include <windows.h>#endif#include <tcl.h>#include "view.h"#include "netview.h"#include "psview.h"#include "editview.h"#include "node.h"#include "queue.h"#include "feature.h"#include "agent.h"#include "edge.h"#include "route.h"#include "monitor.h"#include "lan.h"#include "paint.h"#include "trace.h"//----------------------------------------------------------------------//----------------------------------------------------------------------MovementElement::MovementElement(double time, double x, double y) { next_ = NULL; time_ = time; x_ = x; y_ = y; x_velocity_ = 0.0; y_velocity_ = 0.0; duration_ = 0.0;}//----------------------------------------------------------------------//----------------------------------------------------------------------MovementElement::MovementElement(double time, double x, double y, double x_velocity, double y_velocity, double duration) { next_ = NULL; time_ = time; x_ = x; y_ = y; x_velocity_ = x_velocity; y_velocity_ = y_velocity; duration_ = duration;}//----------------------------------------------------------------------//----------------------------------------------------------------------MovementElement::~MovementElement() { next_ = NULL;}//----------------------------------------------------------------------// bool// MovementElement::contains(double time, double duration);// - returns true if time >= time_ // and < time_ + duration_//----------------------------------------------------------------------boolMovementElement::contains(double time, double duration) { return (time >= time_ && time < (time_ + duration_));}//----------------------------------------------------------------------//----------------------------------------------------------------------MovementList::MovementList() { head_ = NULL;}//----------------------------------------------------------------------// MovementList::~MovementList()// - deletes the remaining part of the list//----------------------------------------------------------------------MovementList::~MovementList() { clear();}//----------------------------------------------------------------------// void// MovementList::clear() {// - deletes all items on the list//----------------------------------------------------------------------voidMovementList::clear() { MovementElement * run; if (head_) { for (run = head_->next_; run; run = run->next_) { delete head_; head_ = run; } if (head_) { delete head_; } head_ = NULL; }}//----------------------------------------------------------------------// MovementList *// MovementList::add(double x, double y, double time)// - adds a new time value to the list which is // sorted in ascending order//----------------------------------------------------------------------MovementElement *MovementList::add(double x, double y, double time) { MovementElement * new_movement = 0, * run = 0, * previous = 0; if (!head_) { // First element on list head_ = new MovementElement(time, x, y); } else { // Have to place down on list previous = NULL; run = head_; for (run = head_; run; run = run->next_) { // slight rounding errors occur with strtod so we need to // have an acceptable error range if ((run->time_ - time <= 0.000001) && (run->time_ - time >= -0.000001)) { // Update time with new x,y run->x_ = x; run->y_ = y; break; } else if (time < run->time_) { if (previous) { // Place in middle of list new_movement = new MovementElement(time, x, y); new_movement->next_ = run; previous->next_ = new_movement; } else { // Add to front of list new_movement = new MovementElement(time, x, y); new_movement->next_ = head_; head_ = new_movement; } break; } previous = run; } if (!run) { // We ran to the end of the list and couldn't place // the new time so we have to place it at the end previous->next_ = new MovementElement(time, x, y); run = previous->next_; } } return run;}//----------------------------------------------------------------------// MovementList * // MovementList::remove(double time)// - removes time from the list //----------------------------------------------------------------------voidMovementList::remove(double time) { MovementElement * run, * previous; previous = NULL; for (run = head_; run; run = run->next_) { // slight rounding errors occur with strtod so we need to // have an acceptable error range if ((run->time_ - time <= 0.000001) && (run->time_ - time >= -0.000001)) { if (previous) { previous->next_ = run->next_; delete run; } else { // Element is at the front of the list head_ = run->next_; } size_--; break; } previous = run; }}//----------------------------------------------------------------------//----------------------------------------------------------------------voidMovementList::setList(char * list_string) { char * run, * next; double time; // Erase all items on the list clear(); // Run down the list string and add values to the list run = list_string; while (run) { time = strtod(run, &next); if (run != next) { //add(time); } else { break; } run = next; }}//----------------------------------------------------------------------// void // MovementList::getListString(char * buffer, int buffer_size)// - fills buffer with a space delimited list of all values on the// time list sorted in ascending order//// - the returned list should be freed when it is not needed anymore//----------------------------------------------------------------------intMovementList::getListString(char * buffer, int buffer_size) { int total_written, just_written; MovementElement * run; total_written = 1; // Need space for the /0 character for (run = head_; run; run = run->next_) { just_written = sprintf(buffer, "%f ", run->time_); if (just_written == -1) { fprintf(stderr, "Ran out of buffer space when creating time list string\n"); total_written = -1; break; } //Advance buffer pointer past written characters buffer += just_written; total_written += just_written; } return total_written;}//----------------------------------------------------------------------////----------------------------------------------------------------------doubleMovementList::lastStopMovement() { MovementElement * element; double stop_time = 0.0; bool start = true; for (element = head(); element; element = element->next_) { if (start) { start = false; } else { stop_time = element->time_; start = true; } } return stop_time;}//----------------------------------------------------------------------// double// MovementList::getXPositionAt(double time)// - Calculates the x position of a node based on where it should be// on it's movement list// - If the list is empty then it doesn't modify x and y////----------------------------------------------------------------------voidMovementList::getPositionAt(double time, double & x, double & y) { MovementElement * before, * after; if (!head()) { return; } before = NULL; for (after = head(); after; after = after->next_) { if (time < after->time_) { break; } before = after; } if (before) { if (after) { // We need to calculate the movement between 2 positions x = before->x_ + (after->x_ - before->x_) * (before->time_ - time) / (before->time_ - after->time_); y = before->y_ + (after->y_ - before->y_) * (before->time_ - time) / (before->time_ - after->time_); } else { // We are at the end of the list so set the position to the // before movement x,y x = before->x_; y = before->y_; } } else { // If there is no before then give the first item in the list before = head(); if (before) { x = before->x_; y = before->y_; } }}//----------------------------------------------------------------------// Wrapper for tcl creation of Nodes//----------------------------------------------------------------------static class NodeClass : public TclClass {public: NodeClass() : TclClass("Node") {} TclObject * create(int argc, const char*const* argv) { Node * node; double size = strtod(argv[6], NULL); // set <node> [new Node node_id type size] if (!strcmp(argv[5], "circle")) { node = new CircleNode(argv[4], size); } else if (!strcmp(argv[5], "box")) { node = new BoxNode(argv[4], size); } else { node = new HexagonNode(argv[4], size); } return node; }} class_node;//----------------------------------------------------------------------//----------------------------------------------------------------------Node::Node(const char* name, double size) : Animation(0, 0), queue_(0), size_(size), nsize_((float) size), x_(0.), y_(0.), x_vel_(0.), y_vel_(0.), starttime_(0.), endtime_(0.), links_(0), routes_(0), agents_(0), anchor_(0), mark_(0), state_(UP), nm_(NULL), nMark_(0), dlabel_(0),// lcolor_(0),// dcolor_(0), // This is some type of hack to not use dcolor// ncolor_(0), direction_(0) { next_ = NULL; label_ = new char[strlen(name) + 1]; strcpy(label_, name); addr_ = nn_ = atoi(name); /*XXX*/ lcolor_ = NULL; dcolor_ = NULL; ncolor_ = NULL; init_color("black"); dx_ = 0.0; dy_ = 0.0; tcl_script_label_ = NULL; tcl_script_ = NULL; wireless_ = false; paint_ = Paint::instance()->thick();}//----------------------------------------------------------------------//----------------------------------------------------------------------Node::~Node() { if (nm_ != NULL) { NodeMark *p = nm_; nm_ = nm_->next; delete p; } delete label_; if (queue_ != NULL) delete queue_; if (tcl_script_) { delete [] tcl_script_; } if (tcl_script_label_) { delete [] tcl_script_label_; }}//----------------------------------------------------------------------// void // Node::init_color(const char *clr) {//----------------------------------------------------------------------void Node::init_color(const char *clr) { color(clr); lcolor(clr); ncolor(clr); dcolor(clr); oldPaint_ = paint_;}void Node::set_down(char *color){ // If current color is down, don't change it again. // Assuming only one down color. User can change this behavior // by adding tcl code for link-up and link-down events. if (state_ == UP) { int pno = Paint::instance()->lookup(color, 3); oldPaint_ = paint_; paint_ = pno; state_ = DOWN; }}void Node::set_up(){ if (state_ == DOWN) { state_ = UP; toggle_color(); }}float Node::distance(float x, float y) const{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -