[メタン の化学構造を、3次元表示する], Python(パイソン) 使って作ってみた ,ソースプログラムリスト あり
Python(パイソン) プログラム作ってみた インデックス へ
-----
2024.9.3 presented in [note] ( //note.com/runningWater/ )
----------
1 はじめに
これ以降に記述されている内容は、このようなコンピューター・プログラムを制作した、というような事を、ただ、述べているに過ぎない。
以下の記述を読んだ人が、それを単に参考にする、というのであれば、問題は無いと、思われる。
しかし、記述されている内容に沿って、その人が、そこに記されているのと同様の制作や作業を行った際に、その制作、作業、コンピューターの作動の結果、使用されたコンピューター等、様々な方面において、何らかの問題が発生しない、という保証は、全くない。
その制作、作業、コンピューターの作動の結果、その人や、その人が所属している組織、その人が使用した様々な機器、インフラストラクチャー等の、身の上にどのような事が起ころうとも、私は一切、責任を負わない。
このプログラムは、Python(パイソン) 言語を使って、記述されている。
----------
2 どのようなものを作ったのか
[メタン分子] の化学構造を、3次元で表示するものを、作ってみた。
[メタン分子] は、1個の炭素原子([C])と、4個の水素原子([H])とで、できており、正四面体形の中心に炭素が、4個の頂点に水素が、というような構造に、なっているのだそうだ。
そこで、そのような位置関係になっている、5個の物体を表示するような、プログラムを作ってみた。このプログラムを動かしてみたら、下図のように、表示された。
----------
3 制作の過程
このプログラムを構成するモジュール群中の多くのものは、下記を構成するモジュール群から、そのままコピーして、流用した。
[断面画像を重ね合わせて、物体を3次元表示], Python(パイソン) 使って作ってみた ,ソースプログラムリスト あり
下記モジュールは、上記からコピーした後に、改変を施して、制作した。
[ ParametersContainer ]
[ ViewSpace ]
[ WorldSpace ]
[ WorldSpace ] から、新たに制作した下記モジュールを、呼び出すような形にした。
[ DrawingOneSphere ]
このモジュールは、原子1個に対する描画のための処理を行うためのものである。
3次元空間上の回転に関する座標変換処理に対しては、下記を新たに制作し、それを使用するようにした。
[ MatrixFor3DRotation ]
[ ParametersContainer ] が使用する、XMLデータ [XML_For_Parameters.xml] は、
[断面画像を重ね合わせて、物体を3次元表示], Python(パイソン) 使って作ってみた ,ソースプログラムリスト あり]
で、使用したものをコピーした後に、下記部分に対して、下記のように修正したものを、使用した。
----------
<value_for_parameters_controling_process>
<value_for_parameters
abs_path_of_base_directory = "E:ForPython/ForTest/Test_5/"
----------
上記の各モジュールの内容について、以降に記す。
----------
4 [ ParametersContainer ] , ファイル名 [ ParametersContainer.py ]
改変した箇所は、以下の通り。
----------
#------------------------------------
# ParametersContainer
#------------------------------------
import XMLHandling
MODULE_NAME = "ParametersContainer"
#****************************
ABS_PATH_OF_XML_FILE = "E:ForPython/ForTest/Test_5/XMLData/XML_For_Parameters.xml"
#****************************
----------
----------
5 [ ViewSpace ] , ファイル名 [ ViewSpace.py ]
----------
import numpy as NUMPY
import ImageDataTwoDimensionHandling
import ParametersContainer
import MatrixFor3DRotaion
import HiddenLineRemoval
#========================================
class ViewSpace :
CV_CLASS_NAME = "ViewSpace"
#------------------------------------------------------------
# cordinate system in World Space
# x + ---> to forward side
# y + ---> to right side
# Z + ---> to upper side
#--View ----------------------------------------------------------
# dordinate system in View Space
# X + ---> to right side
# Y + ---> to upper side
# origin located at left under corner of window
#--------------------------------
# definition of constructer
def __init__( self
, arg_requester_module
, arg_requester_function
) :
methode_name = "constructer"
print ( "==================================" )
print ( "Enter into , Class = " + ViewSpace.CV_CLASS_NAME
+ " , methode = " + methode_name )
print ( " arg_requester_module = " + arg_requester_module
+ " , arg_requester_function = " + arg_requester_function )
print ( "==================================" )
self.iv_ins_MatrixFor3DRotaion = " "
self.iv_rotation_angle_degree_XAxis_to_YAxis = 0
self.iv_rotation_angle_degree_XAxis_to_ZAxis = 0
self.iv_ins_HiddenLineRemoval = " "
#----------------------------------
self.iv_ins_ImageDataTwoDimensionHandling_for_presnting_View_Space = \
ImageDataTwoDimensionHandling \
.ImageDataTwoDimensionHandling ( \
ViewSpace.CV_CLASS_NAME
, methode_name )
#---------------------
self.iv_abs_path_of_ImageDataFile \
= ParametersContainer.global_par_abs_path_of_base_directory \
+ ParametersContainer.global_par_path_of_input_directory \
+ ParametersContainer.global_par_path_of_BaseImageDataForDisplay_directory \
+ ParametersContainer.global_par_file_name_part_1_of_BaseImageDataForDisplay \
+ ParametersContainer.global_par_file_name_ending_part_of_BaseImageDataForDisplay
self.iv_ins_ImageDataTwoDimensionHandling_for_presnting_View_Space \
.load_image_data (
ViewSpace.CV_CLASS_NAME
, methode_name
, self.iv_abs_path_of_ImageDataFile
)
#---------------------
self.iv_width_of_ViewSpace = \
self.iv_ins_ImageDataTwoDimensionHandling_for_presnting_View_Space \
.get_width_of_ImageData ( )
self.iv_height_of_ViewSpace = \
self.iv_ins_ImageDataTwoDimensionHandling_for_presnting_View_Space \
.get_height_of_ImageData ( )
#---------------------
self.iv_half_width_of_ViewSpace = ( int ) ( self.iv_width_of_ViewSpace / 2.0 )
self.iv_half_height_of_ViewSpace = ( int ) ( self.iv_height_of_ViewSpace / 2.0 )
self.iv_x_value_of_perspective_drawing_vanishing_point \
= float ( ParametersContainer \
.global_par_x_value_of_perspective_drawing_vanishing_point )
#------------------------------------------------
print ( "==================================" )
print ( "Exit from , Class = " + ViewSpace.CV_CLASS_NAME
+ " , methode = " + methode_name )
print ( " arg_requester_module = " + arg_requester_module
+ " , arg_requester_function = " + arg_requester_function )
print ( "==================================" )
return
#------------------------------------------------------------
def get_ImageDataTwoDimensionHandling_for_presnting_View_Space ( \
self ) :
return self.iv_ins_ImageDataTwoDimensionHandling_for_presnting_View_Space
#------------------------------------------------------------
def set_rotation_angle_degree ( \
self
, arg_rotation_angle_degree_XAxis_to_YAxis
, arg_rotation_angle_degree_XAxis_to_ZAxis
) :
methode_name = "set_rotation_angle_degree_XAxis_to_YAxis"
self.iv_rotation_angle_degree_XAxis_to_YAxis = \
float ( arg_rotation_angle_degree_XAxis_to_YAxis )
self.iv_rotation_angle_degree_XAxis_to_ZAxis = \
float ( arg_rotation_angle_degree_XAxis_to_ZAxis )
# ------ make rotation instance ---------------
self.iv_ins_MatrixFor3DRotaion = \
MatrixFor3DRotaion \
.MatrixFor3DRotaion (
ViewSpace.CV_CLASS_NAME # arg_requester_module
, methode_name # arg_requester_function
# first rotation
, "Z" # arg_first_rotation_around_axis
, self.iv_rotation_angle_degree_XAxis_to_YAxis
# second rotation
, "Y" # arg_need_Y_N_second_rotation
, "Y" # arg_second_rotation_around_axis
, self.iv_rotation_angle_degree_XAxis_to_ZAxis
)
self.clear_ViewSpace ( )
self.iv_ins_ImageDataTwoDimensionHandling_for_presnting_View_Space \
.insert_text_into_ImageData (
ViewSpace.CV_CLASS_NAME # arg_requester_module
, methode_name # arg_requester_function
# in the commonly used mathematical XY cordinates
, 50 # arg_x_on_ImageData_space
, 50 # arg_y_on_ImageData_space
, "angles have been set" # arg_text_inserted
, 60 # arg_font_color_Blue
, 60 # arg_font_color_Green
, 180 # arg_font_color_Red
)
return \
self.iv_ins_ImageDataTwoDimensionHandling_for_presnting_View_Space
#------------------------------------------------------------
def initializing_process ( \
self
) :
methode_name = "initializing_process"
# reload image_data
self.iv_ins_ImageDataTwoDimensionHandling_for_presnting_View_Space \
.load_image_data (
ViewSpace.CV_CLASS_NAME
, methode_name
, self.iv_abs_path_of_ImageDataFile
)
#--- prepare area for HiddenLineRemoval ---
print ( "==================================" )
print ( "Class = " + ViewSpace.CV_CLASS_NAME
+ " , methode = " + methode_name )
print ( "x_value_of_perspective = " + \
str ( ParametersContainer \
.global_par_x_value_of_perspective_drawing_vanishing_point \
) )
print ( "==================================" )
self.iv_ins_HiddenLineRemoval = \
HiddenLineRemoval \
.HiddenLineRemoval ( \
ViewSpace.CV_CLASS_NAME
, methode_name
, ParametersContainer \
.global_par_x_value_of_perspective_drawing_vanishing_point
, self.iv_width_of_ViewSpace
, self.iv_height_of_ViewSpace
)
return
#------------------------------------------------------------
def clear_ViewSpace ( self ) :
methode_name = "clear_ViewSpace"
self.iv_ins_ImageDataTwoDimensionHandling_for_presnting_View_Space \
.load_image_data (
ViewSpace.CV_CLASS_NAME
, methode_name
, self.iv_abs_path_of_ImageDataFile
)
#------------------------------------------------------------
def draw_one_point_in_ViewSpace ( \
self
, arg_list_x_y_z_in_WorldSpace
, arg_list_color_value_for_drawing
) :
methode_name = "draw_one_point_in_ViewSpace"
# print ( "==================================" )
# print ( "Enter into , Class = " + ViewSpace.CV_CLASS_NAME
# + " , methode = " + methode_name )
# print ( "arg_list_x_y_z_in_WorldSpace = " )
# print ( arg_list_x_y_z_in_WorldSpace )
# print ( "arg_list_color_value_for_drawing = " )
# print ( arg_list_color_value_for_drawing )
# print ( "==================================" )
#-------------- not draw color? -------------------------
if ( ( arg_list_color_value_for_drawing [ 0 ] \
< int ( ParametersContainer.global_par_drawing_color_sleshold ) )\
and \
( arg_list_color_value_for_drawing [ 1 ] \
< int ( ParametersContainer.global_par_drawing_color_sleshold ) )\
and \
( arg_list_color_value_for_drawing [ 2 ] \
< int ( ParametersContainer.global_par_drawing_color_sleshold ) )\
) :
#not draw this point
return "N"
#------------------------------------
drawing_location_in_ViewSpace = \
self.calculate_drawing_location_in_ViewSpace ( \
arg_list_x_y_z_in_WorldSpace
)
# print ( "==================================" )
# print ( "Class = " + ViewSpace.CV_CLASS_NAME
# + " , methode = " + methode_name )
# print ( "arg_list_x_y_z_in_WorldSpace = " )
# print ( arg_list_x_y_z_in_WorldSpace )
# print ( "drawing_location_in_ViewSpace = " )
# print ( drawing_location_in_ViewSpace )
# print ( "==================================" )
it_must_be_drawed_Y_N_about_hidden_line = \
self.iv_ins_HiddenLineRemoval \
.detect_and_set_x_value_of_directed_location ( \
ViewSpace.CV_CLASS_NAME
, methode_name
, drawing_location_in_ViewSpace [ 0 ] # arg_X_in ViewSpace
, drawing_location_in_ViewSpace [ 1 ] # arg_Y_in ViewSpace
, arg_list_x_y_z_in_WorldSpace [ 0 ] #arg_x_in_WorldSpace
)
# print ( "==================================" )
# print ( "Class = " + ViewSpace.CV_CLASS_NAME
# + " , methode = " + methode_name )
# print ( "it_must_be_drawed_Y_N_about_hidden_line = " \
# , it_must_be_drawed_Y_N_about_hidden_line )
# print ( "==================================" )
if ( it_must_be_drawed_Y_N_about_hidden_line == "Y" ) :
# ------ draw one point --------------
self.draw_one_point_at_directed_location_in_ViewSpace ( \
[ drawing_location_in_ViewSpace [ 0 ] \
, drawing_location_in_ViewSpace [ 1 ] \
]
, arg_list_color_value_for_drawing
)
return "Y"
return "N"
#------------------------------------------------------------
def draw_axis_line ( \
self
, arg_axis_x_y_z
) :
methode_name = "draw_axis_line"
# print ( "==================================" )
# print ( "Enter into , Class = " + HiddenLineRemoval.CV_CLASS_NAME
# + " , methode = " + methode_name )
# print ( " arg_requester_module = " + arg_requester_module
# + " , arg_requester_function = " + arg_requester_function )
# print ( " arg_X_in ViewSpace = " \
# + str ( arg_X_in ViewSpace )
# print ( " arg_Y_in ViewSpace = " + str ( arg_Y_in ViewSpace ) )
# print ( " arg_x_in_WorldSpace = " + str ( arg_x_in_WorldSpace ) )
# print ( "==================================" )
list_start_point_x_y_z = [ 0 , 0 , 0 ]
list_end_point_x_y_z = [ 0 , 0 , 0 ]
if ( arg_axis_x_y_z == "X" ) :
list_end_point_x_y_z [ 0 ] = 120
if ( arg_axis_x_y_z == "Y" ) :
list_end_point_x_y_z [ 1 ] = 120
if ( arg_axis_x_y_z == "Z" ) :
list_end_point_x_y_z [ 2 ] = 120
#------------------------------------
drawing_location_start_point_in_ViewSpace = \
self.calculate_drawing_location_in_ViewSpace ( \
list_start_point_x_y_z
)
drawing_location_end_point_in_ViewSpace = \
self.calculate_drawing_location_in_ViewSpace ( \
list_end_point_x_y_z
)
self.iv_ins_ImageDataTwoDimensionHandling_for_presnting_View_Space \
.insert_line_figure_into_ImageData (
ViewSpace.CV_CLASS_NAME # arg_requester_module
, methode_name # arg_requester_function
# in the commonly used mathematical XY cordinates
, drawing_location_start_point_in_ViewSpace [ 0 ] \
# arg_From_x_on_ImageData_space
, drawing_location_start_point_in_ViewSpace [ 1 ] \
# arg_From_y_on_ImageData_space
, drawing_location_end_point_in_ViewSpace [ 0 ] \
# arg_To_x_on_ImageData_space
, drawing_location_end_point_in_ViewSpace [ 1 ] \
# arg_To_y_on_ImageData_space
, 3 # arg_thickness_of_line_figure
, 20 # arg_color_Blue
, 10 # arg_color_Green
, 220 # arg_color_Red
)
self.iv_ins_ImageDataTwoDimensionHandling_for_presnting_View_Space \
.insert_text_into_ImageData (
ViewSpace.CV_CLASS_NAME # arg_requester_module
, methode_name # arg_requester_function
# in the commonly used mathematical XY cordinates
, drawing_location_end_point_in_ViewSpace [ 0 ] \
# arg_To_x_on_ImageData_space
, drawing_location_end_point_in_ViewSpace [ 1 ] \
, arg_axis_x_y_z
, 20 # arg_color_Blue
, 10 # arg_color_Green
, 220 # arg_color_Red
)
return
#------------------------------------------------------------
def calculate_drawing_location_in_ViewSpace ( \
self
, arg_list_x_y_z_in_WorldSpace
) :
# do rotation
list__x_y_z_after_rotation = \
self.iv_ins_MatrixFor3DRotaion \
.calculate_cordinate_after_rotation ( \
arg_list_x_y_z_in_WorldSpace
)
# x in WorldSpace
w1 = list__x_y_z_after_rotation [ 0 ] \
- self.iv_x_value_of_perspective_drawing_vanishing_point
w2 = 0 - self.iv_x_value_of_perspective_drawing_vanishing_point
rate_of_magnification = w1 / w2
w1_X = list__x_y_z_after_rotation [ 1 ] # y cordinate value
w1_Y = list__x_y_z_after_rotation [ 2 ] # z cordinate value
w2_X = w1_X * rate_of_magnification
w2_Y = w1_Y * rate_of_magnification
drawing_location_X_in_ViewSpace = \
int ( self.iv_half_width_of_ViewSpace + w2_X )
drawing_location_Y_in_ViewSpace = \
int ( self.iv_half_height_of_ViewSpace + w2_Y )
return_list = [ drawing_location_X_in_ViewSpace \
, drawing_location_Y_in_ViewSpace ]
return return_list
#------------------------------------------------------------
def draw_one_point_at_directed_location_in_ViewSpace ( \
self
, arg_list_X_Y_in_ViewSpace
, arg_list_color_value_for_drawing
) :
methode_name = "draw_one_point_at_directed_location_in_ViewSpace"
# print ( "==================================" )
# print ( "Class = " + ViewSpace.CV_CLASS_NAME
# + " , methode = " + methode_name )
# print ( "arg_list_X_Y_in_ViewSpace = " )
# print ( arg_list_X_Y_in_ViewSpace )
# print ( "arg_list_color_value_for_drawing = " )
# print ( arg_list_color_value_for_drawing )
# print ( "==================================" )
self.iv_ins_ImageDataTwoDimensionHandling_for_presnting_View_Space \
.put_data_of_one_pixel ( \
ViewSpace.CV_CLASS_NAME # arg_requester_module
, methode_name # arg_requester_function
# in the commonly used mathematical XY cordinates
, arg_list_X_Y_in_ViewSpace [ 0 ] # arg_x_on_ImageData_space
, arg_list_X_Y_in_ViewSpace [ 1 ] # arg_Y_on_ImageData_space
, arg_list_color_value_for_drawing [ 0 ] # arg_color_data_Blue
, arg_list_color_value_for_drawing [ 1 ] # arg_color_data_Green
, arg_list_color_value_for_drawing [ 2 ] # arg_color_data_Red
)
----------
----------
6 [ WorldSpace ] , ファイル名 [ WorldSpace.py ]
----------
import DrawingOneSphere
#========================================
class WorldSpace :
CV_CLASS_NAME = "WorldSpace"
#------------------------------------------------------------
# cordinate system in World Space
# x + ---> to forward side
# y + ---> to right side
# Z + ---> to upper side
#--------------------------------
# definition of constructer
def __init__( self
, arg_requester_module
, arg_requester_function
) :
methode_name = "constructer"
print ( "==================================" )
print ( "Enter into , Class = " + WorldSpace.CV_CLASS_NAME
+ " , methode = " + methode_name )
print ( " arg_requester_module = " + arg_requester_module
+ " , arg_requester_function = " + arg_requester_function )
print ( "==================================" )
print ( "==================================" )
print ( "Exit from , Class = " + WorldSpace.CV_CLASS_NAME
+ " , methode = " + methode_name )
print ( " arg_requester_module = " + arg_requester_module
+ " , arg_requester_function = " + arg_requester_function )
print ( "==================================" )
#------------------------------------------------------------
def initializing_process ( self ) :
methode_name = "initializing_process"
print ( "==================================" )
print ( "Enter into , Class = " + WorldSpace.CV_CLASS_NAME \
+ " , methode = " + methode_name )
print ( "==================================" )
#*******************************
# define C and H4
#*******************************
self.iv_number_of_Atoms = 5 # ( C + 4 H )
list_basic_drawing_color_of_C = [ 200 , 200 , 60 ]
list_basic_drawing_color_of_H = [ 20 , 200 , 200 ]
radius_of_C = 50
radius_of_H = 40
value_for_deciding_location_H = 100
# add center location of C atom
list_center_location_xyz = [ ]
list_center_location_xyz \
.append ( [ 0 , 0 , 0 ] )
list_center_location_xyz \
.append ( [ \
value_for_deciding_location_H
, value_for_deciding_location_H
, value_for_deciding_location_H ] )
# add center location of 4 H atoms
list_center_location_xyz \
.append ( [ \
value_for_deciding_location_H
, ( 0 - value_for_deciding_location_H )
, ( 0 - value_for_deciding_location_H ) ] )
list_center_location_xyz \
.append ( [ \
( 0 - value_for_deciding_location_H )
, value_for_deciding_location_H
, ( 0 - value_for_deciding_location_H ) ] )
list_center_location_xyz \
.append ( [ \
( 0 - value_for_deciding_location_H )
, ( 0 - value_for_deciding_location_H )
, value_for_deciding_location_H ] )
print ( "list_center_location_xyz = " )
print ( list_center_location_xyz )
# ----------------------------------
self.iv_list_of_DrawingOneSphere = [ ]
#---------- C atom ---------------------
ins_DrawingOneSphere_C \
= DrawingOneSphere \
.DrawingOneSphere ( \
1
, radius_of_C
, list_center_location_xyz [ 0 ]
, list_basic_drawing_color_of_C )
self.iv_list_of_DrawingOneSphere \
.append ( ins_DrawingOneSphere_C )
#---------- 4 H atom ---------------------
for i in range ( 1 , self.iv_number_of_Atoms ) :
self.iv_list_of_DrawingOneSphere \
.append ( \
DrawingOneSphere \
.DrawingOneSphere (
( i + 1 ) \
, radius_of_H
, list_center_location_xyz [ i ]
, list_basic_drawing_color_of_H \
) \
)
self.iv_size_of_list_DrawingOneSphere = \
len ( self.iv_list_of_DrawingOneSphere )
#-------------------------
self.iv_current_number_of_DrawingOneSphere = 1
self.iv_ins_DrawingOneSphere_current_drawing = \
self.get_DrawingOneSphere ( self.iv_current_number_of_DrawingOneSphere )
#-----------------------------------
self.conversion_rate_from_WorldSpace_to_ViewSpace = 0.7
self.iv_next_continue = " "
print ( "==================================" )
print ( "Exit from , Class = " + WorldSpace.CV_CLASS_NAME \
+ " , methode = " + methode_name )
print ( "==================================" )
#------------------------------------------------------------
def get_DrawingOneSphere ( self , arg_number_of_DrawingOneSphere ) :
methode_name = "get_DrawingOneSphere"
return self.iv_list_of_DrawingOneSphere [ \
arg_number_of_DrawingOneSphere - 1 ]
#------------------------------------------------------------
def make_one_point_on_WorldSpace ( self ) :
methode_name = "make_one_point_on_WorldSpace"
# print ( "==================================" )
# print ( "Enter into Class = " + WorldSpace.CV_CLASS_NAME \
# + " , methode = " + methode_name )
# print ( "id of instance DrawingOneSphere = " \
# , self.iv_ins_DrawingOneSphere_current_drawing \
# .get_id ( ) )
# print ( "==================================" )
return_list = self.iv_ins_DrawingOneSphere_current_drawing \
.get_information_of_current_drawing_location ( )
# print ( "==================================" )
# print ( "Class = " + WorldSpace.CV_CLASS_NAME \
# + " , methode = " + methode_name )
# print ( "return_list = " \
# , return_list )
# print ( "==================================" )
#---------------------------
if ( return_list [ 0 ] != "Y" ) :
# process end of current DrawingOneSphere
if ( self.iv_current_number_of_DrawingOneSphere \
== self.iv_size_of_list_DrawingOneSphere ) :
#all process end
return return_list
#----------------------------------
self.iv_current_number_of_DrawingOneSphere += 1
self.iv_ins_DrawingOneSphere_current_drawing = \
self.get_DrawingOneSphere ( self.iv_current_number_of_DrawingOneSphere )
print ( "==================================" )
print ( "Class = " + WorldSpace.CV_CLASS_NAME \
+ " , methode = " + methode_name )
print ( "return_list = " \
, return_list )
print ( "id of instance DrawingOneSphere = " \
, self.iv_ins_DrawingOneSphere_current_drawing \
.get_id ( ) )
print ( "==================================" )
return_list = self.iv_ins_DrawingOneSphere_current_drawing \
.get_information_of_current_drawing_location ( )
print ( "==================================" )
print ( "Class = " + WorldSpace.CV_CLASS_NAME \
+ " , methode = " + methode_name )
print ( "self.iv_current_number_of_DrawingOneSphere = " \
, self.iv_current_number_of_DrawingOneSphere )
print ( "center_location_xyz of this instance = " )
print ( \
self.iv_ins_DrawingOneSphere_current_drawing \
.get_list_center_location_xyz ( ) \
)
print ( "==================================" )
#----------------------------------
# decide x , y , z for return
# print ( "==================================" )
# print ( "Class = " + WorldSpace.CV_CLASS_NAME \
# + " , methode = " + methode_name )
# print ( "self.conversion_rate_from_WorldSpace_to_ViewSpace = " \
# , self.conversion_rate_from_WorldSpace_to_ViewSpace )
# print ( "return_list = " )
# print ( return_list )
# print ( "==================================" )
# x value
return_list [ 1 ] \
*= self.conversion_rate_from_WorldSpace_to_ViewSpace
# y value
return_list [ 2 ] \
*= self.conversion_rate_from_WorldSpace_to_ViewSpace
# z value
return_list [ 3 ] \
*= self.conversion_rate_from_WorldSpace_to_ViewSpace
# print ( "==================================" )
# print ( "Class = " + WorldSpace.CV_CLASS_NAME \
# + " , methode = " + methode_name )
# print ( "return_list = " )
# print ( return_list )
# print ( "==================================" )
return return_list
----------
----------
7 [ DrawingOneSphere ] , ファイル名 [ DrawingOneSphere.py ]
----------
import math
import MatrixFor3DRotaion
#========================================
class DrawingOneSphere :
CV_CLASS_NAME = "DrawingOneSphere"
#------------------------------------------------------------
# cordinate system in World Space
# x + ---> to forward side
# y + ---> to right side
# Z + ---> to upper side
#--------------------------------
# definition of constructer
def __init__( self
, arg_id
, arg_radius
, arg_list_center_location_xyz
, arg_list_basic_drawing_color
) :
methode_name = "constructer"
print ( "==================================" )
print ( "Enter into , Class = " + DrawingOneSphere.CV_CLASS_NAME
+ " , methode = " + methode_name )
print ( "arg_id = " , arg_id )
print ( "arg_radius = " , arg_radius )
print ( "arg_list_center_location_xyz = " , arg_list_center_location_xyz )
print ( "arg_list_basic_drawing_color = " , arg_list_basic_drawing_color )
print ( "==================================" )
self.iv_id = arg_id
# ------------------------------
self.iv_radius = arg_radius
self.iv_list_center_location_xyz = [ \
arg_list_center_location_xyz [ 0 ] \
, arg_list_center_location_xyz [ 1 ] \
, arg_list_center_location_xyz [ 2 ] \
]
self.iv_list_basic_drawing_color = [ \
arg_list_basic_drawing_color [ 0 ] \
, arg_list_basic_drawing_color [ 1 ] \
, arg_list_basic_drawing_color [ 2 ] \
]
# -------------------------------------
# -------------------------------------
self.iv_number_of_drawing_lines_per_0_to_2PI = 150
# -------------------------------------
# -------------------------------------
self.iv_angle_radian_YZ_from = 0.0
self.iv_angle_radian_YZ_to = 2.0 * math.pi
self.iv_angle_radian_YZ_incliment = \
( self.iv_angle_radian_YZ_to \
- self.iv_angle_radian_YZ_from ) \
/ float ( self.iv_number_of_drawing_lines_per_0_to_2PI )
self.iv_current_angle_radian_YZ = self.iv_angle_radian_YZ_from
self.iv_next_angle_radian_YZ = self.iv_current_angle_radian_YZ
# -------------------------------------
self.iv_angle_radian_XY_from = 0.0
self.iv_angle_radian_XY_to = 2.0 * math.pi
self.iv_angle_radian_XY_incliment = \
( self.iv_angle_radian_XY_to \
- self.iv_angle_radian_XY_from ) \
/ float ( self.iv_number_of_drawing_lines_per_0_to_2PI )
self.iv_current_angle_radian_XY = self.iv_angle_radian_XY_from
self.iv_next_angle_radian_XY = self.iv_current_angle_radian_XY
# -------------------------------
self.iv_need_make_Matrix_for_rotation = "Y"
#-----------------------------------------
self.iv_BrightnessAdjustmentValue_Min = 0.3
self.iv_BrightnessAdjustmentValue_Max = 0.99
self.iv_BrightnessAdjustmentValue_Max_minus_Min = \
self.iv_BrightnessAdjustmentValue_Max - \
self.iv_BrightnessAdjustmentValue_Min
print ( "==================================" )
print ( "Exit from , Class = " + DrawingOneSphere.CV_CLASS_NAME
+ " , methode = " + methode_name )
print ( "==================================" )
#------------------------------------------------------------
def get_list_center_location_xyz ( self ) :
return self.iv_list_center_location_xyz
#------------------------------------------------------------
def get_id ( self ) :
return self.iv_id
#------------------------------------------------------------
def get_information_of_current_drawing_location ( self ) :
methode_name = "get_information_of_current_drawing_location"
# print ( "==================================" )
# print ( "Enter into Class = " + DrawingOneSphere.CV_CLASS_NAME
# + " , methode = " + methode_name )
# print ( "==================================" )
return_list = [ \
"Y" #process continue \
, 0 #x \
, 0 #y \
, 0 #z \
#--- color --
, 0 # Blue \
, 0 # Green \
, 0 # Red \
]
if ( self.iv_need_make_Matrix_for_rotation == "Y" ) :
self.prepare_for_rotation_XY ( self.iv_current_angle_radian_XY )
# -------------------------
result = self.decide_next_angle ( )
# print ( "==================================" )
# print ( "Class = " + DrawingOneSphere.CV_CLASS_NAME
# + " , methode = " + methode_name )
# print ( "result = " , result )
# print ( "==================================" )
if ( result == "N" ) :
# reached to process end
return_list [ 0 ] = "N"
return return_list
# decide xyz, using current information
calculated_list_xyz = self.calculate_xyz ( )
return_list [ 1 ] = calculated_list_xyz [ 0 ]
return_list [ 2 ] = calculated_list_xyz [ 1 ]
return_list [ 3 ] = calculated_list_xyz [ 2 ]
# decide color
decided_color = self.decide_color ( calculated_list_xyz [ 4 ] )
return_list [ 4 ] = decided_color [ 0 ]
return_list [ 5 ] = decided_color [ 1 ]
return_list [ 6 ] = decided_color [ 2 ]
# prepare next call , next information --> current information
self.iv_current_angle_radian_YZ = self.iv_next_angle_radian_YZ
self.iv_current_angle_radian_XY = self.iv_next_angle_radian_XY
#-----------------------
# print ( "==================================" )
# print ( "Class = " + DrawingOneSphere.CV_CLASS_NAME
# + " , methode = " + methode_name )
# print ( "calculated_list_xyz = " , calculated_list_xyz )
# print ( "decided_color = " , decided_color )
# print ( "==================================" )
return return_list
#------------------------------------------------------------
def decide_next_angle ( self ) :
methode_name = "decide_next_angle"
# print ( "==================================" )
# print ( "Enter into Class = " + DrawingOneSphere.CV_CLASS_NAME
# + " , methode = " + methode_name )
# print ( "==================================" )
return_value = "Y"
self.iv_next_continue = "Y"
self.iv_next_angle_radian_YZ = self.iv_current_angle_radian_YZ
self.iv_next_angle_radian_XY = self.iv_current_angle_radian_XY
#---------------------------
w_angle_radian = \
self.iv_current_angle_radian_YZ \
+ self.iv_angle_radian_YZ_incliment
# print ( "==================================" )
# print ( "Class = " + DrawingOneSphere.CV_CLASS_NAME
# + " , methode = " + methode_name )
# print ( "self.iv_current_angle_radian_YZ = " , self.iv_current_angle_radian_YZ )
# print ( "self.iv_angle_radian_YZ_incliment = " , self.iv_angle_radian_YZ_incliment )
# print ( "w_angle_radian = " , w_angle_radian )
# print ( "self.iv_angle_radian_YZ_to = " , self.iv_angle_radian_YZ_to )
# print ( "==================================" )
if ( w_angle_radian <= self.iv_angle_radian_YZ_to ) :
self.iv_next_angle_radian_YZ = w_angle_radian
self.iv_need_make_Matrix_for_rotation_XY = "N"
return return_value
#--------- reset angle_YZ to from value , and angle_XY plus ------------------
self.iv_need_make_Matrix_for_rotation_XY = "Y"
self.iv_next_angle_radian_YZ = self.iv_angle_radian_YZ_from
w_angle_radian = self.iv_current_angle_radian_XY \
+ self.iv_angle_radian_XY_incliment
if ( w_angle_radian <= self.iv_angle_radian_XY_to ) :
# ---- angle_XY plus ------------------
self.iv_next_angle_radian_XY = w_angle_radian
# print ( "==================================" )
# print ( "Class = " + DrawingOneSphere.CV_CLASS_NAME \
# + " , methode = " + methode_name )
# print ( "self.iv_current_angle_radian_XY = " \
# , self.iv_current_angle_radian_XY )
# print ( "self.iv_angle_radian_XY_incliment = " \
# , self.iv_angle_radian_XY_incliment )
# print ( "w_angle_radian = " \
# , w_angle_radian )
# print ( "self.iv_angle_radian_XY_to = " \
# , self.iv_angle_radian_XY_to )
# print ( "self.iv_next_angle_radian_XY = " \
# , self.iv_next_angle_radian_XY )
# print ( "==================================" )
return return_value
#------ process end ---------------------
self.iv_need_make_Matrix_for_rotation_XY = "N"
self.iv_next_continue = "N"
return_value = "N"
return return_value
#------------------------------------------------------------
def calculate_xyz ( self ) :
methode_name = "calculate_xyz"
return_list = [ 0.0 , 0.0 , 0.0 , 0.0 , 0.0 , 0.0 ]
list_xyz_before_rotation = [ 0 , 0 , 0 ]
# x
list_xyz_before_rotation [ 0 ] = 0.0
# y
list_xyz_before_rotation [ 1 ] = \
self.iv_radius * \
( math.cos ( self.iv_current_angle_radian_YZ ) )
# z
list_xyz_before_rotation [ 2 ] = \
self.iv_radius * \
( math.sin ( self.iv_current_angle_radian_YZ ) )
# print ( "list_xyz_before_rotation = " )
# print ( list_xyz_before_rotation )
list_xyz_after_rotation = \
self.iv_ins_MatrixFor3DRotaion_XY \
.calculate_cordinate_after_rotation ( \
list_xyz_before_rotation
)
# print ( "==================================" )
# print ( "Class = " + DrawingOneSphere.CV_CLASS_NAME \
# + " , methode = " + methode_name )
# print ( "list_xyz_before_rotation = " )
# print ( list_xyz_before_rotation )
# print ( "list_xyz_after_rotation = " )
# print ( list_xyz_after_rotation )
# print ( "==================================" )
# --------------------------------------
# x cordinate
return_list [ 0 ] = self.iv_list_center_location_xyz [ 0 ] \
+ list_xyz_after_rotation [ 0 ]
return_list [ 3 ] = list_xyz_after_rotation [ 0 ]
# y cordinate
return_list [ 1 ] = self.iv_list_center_location_xyz [ 1 ] \
+ list_xyz_after_rotation [ 1 ]
return_list [ 4 ] = list_xyz_after_rotation [ 1 ]
# z cordinate
return_list [ 2 ] = self.iv_list_center_location_xyz [ 2 ] \
+ list_xyz_after_rotation [ 2 ]
return_list [ 5 ] = list_xyz_after_rotation [ 2 ]
return return_list
#--------------------------------------------------
def prepare_for_rotation_XY ( self \
, arg_angle_radian ) :
methode_name = "prepare_for_rotation_XY"
value_degree = arg_angle_radian * ( 180.0 / math.pi )
self.iv_ins_MatrixFor3DRotaion_XY = \
MatrixFor3DRotaion \
.MatrixFor3DRotaion ( \
DrawingOneSphere.CV_CLASS_NAME
, methode_name
, "Z" # rotatote X -- Y , around Z-Axis
, value_degree
, "N" # arg_need_Y_N_second_rotation
, " " # arg_second_rotation_around_axis
, 0.0 # arg_second_rotation_angle_degree
)
#------------------------------------------------------------
def decide_color ( self , arg_value ) :
methode_name = "decide_color"
most_left = 0.0 - self.iv_radius
most_right = self.iv_radius
fron_most_left_to_most_right = float ( most_right - most_left )
w_value_from_left = float ( arg_value - most_left )
w_rate_about_value = w_value_from_left / fron_most_left_to_most_right
rate_about_value = w_rate_about_value
if ( w_rate_about_value < 0.01 ) :
rate_about_value = 0.01
if ( w_rate_about_value > 0.99 ) :
rate_about_value = 0.99
value_BrightnessAdjustment = \
self.iv_BrightnessAdjustmentValue_Min + \
( self.iv_BrightnessAdjustmentValue_Max_minus_Min \
* rate_about_value )
# print ( "==================================" )
# print ( "Class = " + DrawingOneSphere.CV_CLASS_NAME \
# + " , methode = " + methode_name )
# print ( "value_BrightnessAdjustment = " \
# , value_BrightnessAdjustment )
# print ( "==================================" )
return_list = [ 0 , 0 , 0 ]
return_list [ 0 ] = int ( \
self.iv_list_basic_drawing_color [ 0 ] \
* value_BrightnessAdjustment \
)
return_list [ 1 ] = int ( \
self.iv_list_basic_drawing_color [ 1 ] \
* value_BrightnessAdjustment \
)
return_list [ 2 ] = int ( \
self.iv_list_basic_drawing_color [ 2 ] \
* value_BrightnessAdjustment \
)
return return_list
----------
----------
8 [ MatrixFor3DRotation ] , ファイル名 [ MatrixFor3DRotation.py ]
----------
import numpy as NUMPY
import math
#===========================================
class MatrixFor3DRotaion :
CV_CLASS_NAME = "MatrixFor3DRotaion"
#------------------------------------------------------------
# definition of constructer
def __init__( self
, arg_requester_module
, arg_requester_function
#when doing first rotation, around, what axis?
, arg_first_rotation_around_axis
#when doing first rotation, how dgree, used?
, arg_first_rotation_angle_degree
, arg_need_Y_N_second_rotation
#when doing second rotation, around, what axis?
, arg_second_rotation_around_axis
#when doing second rotation, how dgree, used?
, arg_second_rotation_angle_degree
) :
methode_name = "constructer"
# print ( "==================================" )
# print ( "Enter into , Class = " + MatrixFor3DRotaion.CV_CLASS_NAME
# + " , methode = " + methode_name )
# print ( " arg_requester_module = " + arg_requester_module
# + " , arg_requester_function = " + arg_requester_function )
# print ( "==================================" )
self.iv_first_rotation_around_axis = arg_first_rotation_around_axis
self.iv_first_rotation_angle_degree = \
arg_first_rotation_angle_degree
self.iv_first_rotation_angle_radian \
= self.iv_first_rotation_angle_degree \
* ( math.pi / 180.0 )
self.iv_ins_MatrixUsingNUMPY_for_first_rotation \
= self.make_matrix ( \
self.iv_first_rotation_around_axis
, self.iv_first_rotation_angle_radian
)
self.iv_need_Y_N_second_rotation = arg_need_Y_N_second_rotation
if ( self.iv_need_Y_N_second_rotation != "Y" ) :
self.iv_ins_MatrixUsingNUMPY_for_rotation \
= self.iv_ins_MatrixUsingNUMPY_for_first_rotation
self.iv_second_rotation_around_axis = " "
self.iv_second_rotation_angle_degree = 0
self.iv_second_rotation_angle_radian = 0
# ******************************************
# print ( "self.iv_need_Y_N_second_rotation = " \
# , self.iv_need_Y_N_second_rotation )
# ******************************************
# print ( "self.iv_ins_MatrixUsingNUMPY_for_first_rotation = " )
# print ( self.iv_ins_MatrixUsingNUMPY_for_first_rotation )
# print ( "self.iv_ins_MatrixUsingNUMPY_for_rotation = " )
# print ( self.iv_ins_MatrixUsingNUMPY_for_rotation )
# print ( "==================================" )
# print ( "Exit from , Class = " + MatrixFor3DRotaion.CV_CLASS_NAME
# + " , methode = " + methode_name )
# print ( " arg_requester_module = " + arg_requester_module
# + " , arg_requester_function = " + arg_requester_function )
# print ( "==================================" )
return
else : # need_Y_N_second_rotation
self.iv_second_rotation_around_axis = \
arg_second_rotation_around_axis
self.iv_second_rotation_angle_degree = \
arg_second_rotation_angle_degree
self.iv_second_rotation_angle_radian \
= self.iv_second_rotation_angle_degree \
* ( math.pi / 180.0 )
self.iv_ins_MatrixUsingNUMPY_for_second_rotation \
= self.make_matrix ( \
self.iv_second_rotation_around_axis
, self.iv_second_rotation_angle_radian
)
# make matrix of multiplaying first and second rotation
self.iv_ins_MatrixUsingNUMPY_for_rotation \
= NUMPY.matmul ( \
self.iv_ins_MatrixUsingNUMPY_for_second_rotation
, self.iv_ins_MatrixUsingNUMPY_for_first_rotation
)
# print ( "self.iv_need_Y_N_second_rotation = " \
# + self.iv_need_Y_N_second_rotation )
# print ( "self.iv_ins_MatrixUsingNUMPY_for_second_rotation = " )
# print ( self.iv_ins_MatrixUsingNUMPY_for_second_rotation )
# print ( "self.iv_ins_MatrixUsingNUMPY_for_first_rotation = " )
# print ( self.iv_ins_MatrixUsingNUMPY_for_first_rotation )
# print ( "self.iv_ins_MatrixUsingNUMPY_for_rotation = " )
# print ( self.iv_ins_MatrixUsingNUMPY_for_rotation )
# print ( "==================================" )
# print ( "Exit from , Class = " + MatrixFor3DRotaion.CV_CLASS_NAME
# + " , methode = " + methode_name )
# print ( " arg_requester_module = " + arg_requester_module
# + " , arg_requester_function = " + arg_requester_function )
# print ( "==================================" )
return
# ---------------------------------------
# print ( "==================================" )
# print ( "Exit from , Class = " + MatrixFor3DRotaion.CV_CLASS_NAME
# + " , methode = " + methode_name )
# print ( " arg_requester_module = " + arg_requester_module
# + " , arg_requester_function = " + arg_requester_function )
# print ( "==================================" )
#------------------------------------------------------------
def calculate_cordinate_after_rotation ( self \
, arg_list_cordinate_before_rotation
) :
methode_name = "calculate_cordinate_after_rotation"
# print ( "==================================" )
# print ( "Enter into , Class = " + MatrixFor3DRotaion.CV_CLASS_NAME
# + " , methode = " + methode_name )
# print ( " arg_list_cordinate_before_rotation = " )
# print ( arg_list_cordinate_before_rotation )
# print ( "==================================" )
cordinate_before_NUMPY = \
NUMPY.array ( \
[ \
[ arg_list_cordinate_before_rotation [ 0 ] ] \
, [ arg_list_cordinate_before_rotation [ 1 ] ] \
, [ arg_list_cordinate_before_rotation [ 2 ] ] \
] \
)
return_matrixUsingNUMPY = NUMPY.matmul ( \
self.iv_ins_MatrixUsingNUMPY_for_rotation
,cordinate_before_NUMPY
)
# print ( "==================================" )
# print ( "Class = " + MatrixFor3DRotaion.CV_CLASS_NAME
# + " , methode = " + methode_name )
# print ( "==================================" )
# print ( "cordinate_before_NUMPY = " )
# print ( cordinate_before_NUMPY )
# print ( "self.iv_ins_MatrixUsingNUMPY_for_rotation = " )
# print ( self.iv_ins_MatrixUsingNUMPY_for_rotation )
# print ( "return_matrixUsingNUMPY = " )
# print ( return_matrixUsingNUMPY )
# print ( "************************" )
# print ( "matmul done" )
# print ( "************************" )
#*********************************
return_list = [ \
float ( return_matrixUsingNUMPY [ 0 ] [ 0 ] ) \
, float ( return_matrixUsingNUMPY [ 1 ] [ 0 ] ) \
, float ( return_matrixUsingNUMPY [ 2 ] [ 0 ] ) \
]
# return return_matrixUsingNUMPY
return return_list
#*********************************
#------------------------------------------------------------
def make_matrix ( self \
, arg_rotation_around_axis
, arg_rotation_angle_radian
) :
methode_name = "make_matrix"
rotation_matrix_value = [
[ 1 , 0 , 0 ] ,
[ 0 , 1 , 0 ] ,
[ 0 , 0 , 1 ]
]
if ( arg_rotation_around_axis == "Z" ) :
#rotation around Z axis
# direction X axis --> Y axis
#--------- row 1 ---------------
rotation_matrix_value [ 1 - 1 ] [ 1 - 1 ] \
= math.cos ( arg_rotation_angle_radian )
rotation_matrix_value [ 1 - 1 ] [ 2 - 1 ] \
= ( 0.0 - math.sin ( arg_rotation_angle_radian ) )
rotation_matrix_value [ 1 - 1 ] [ 3 - 1 ] \
= 0.0
#---------- row 2 --------------
rotation_matrix_value [ 2 - 1 ] [ 1 - 1 ] \
= math.sin ( arg_rotation_angle_radian )
rotation_matrix_value [ 2 - 1 ] [ 2 - 1 ] \
= math.cos ( arg_rotation_angle_radian )
rotation_matrix_value [ 2 - 1 ] [ 3 - 1 ] \
= 0.0
#---------- row 3 --------------
rotation_matrix_value [ 3 - 1 ] [ 1 - 1 ] \
= 0.0
rotation_matrix_value [ 3 - 1 ] [ 2 - 1 ] \
= 0.0
rotation_matrix_value [ 3 - 1 ] [ 3 - 1 ] \
= 1.0
return_matrixUsingNUMPY \
= NUMPY.array ( rotation_matrix_value )
return return_matrixUsingNUMPY
#---------------------------------------------------
if ( arg_rotation_around_axis == "Y" ) :
#rotation around Y axis
# direction X axis --> Z axis
#--------- row 1 ---------------
rotation_matrix_value [ 1 - 1 ] [ 1 - 1 ] \
= math.cos ( arg_rotation_angle_radian )
rotation_matrix_value [ 1 - 1 ] [ 2 - 1 ] \
= 0.0
rotation_matrix_value [ 1 - 1 ] [ 3 - 1 ] \
= math.sin ( arg_rotation_angle_radian )
#---------- row 2 --------------
rotation_matrix_value [ 2 - 1 ] [ 1 - 1 ] \
= 0.0
rotation_matrix_value [ 2 - 1 ] [ 2 - 1 ] \
= 1.0
rotation_matrix_value [ 2 - 1 ] [ 3 - 1 ] \
= 0.0
#---------- row 3 --------------
rotation_matrix_value [ 3 - 1 ] [ 1 - 1 ] \
= 0.0 - math.sin ( arg_rotation_angle_radian )
rotation_matrix_value [ 3 - 1 ] [ 2 - 1 ] \
= 0.0
rotation_matrix_value [ 3 - 1 ] [ 3 - 1 ] \
= math.cos ( arg_rotation_angle_radian )
return_matrixUsingNUMPY \
= NUMPY.array ( rotation_matrix_value )
return return_matrixUsingNUMPY
#---------------------------------------------------
if ( arg_rotation_around_axis == "X" ) :
#rotation around X axis
# direction Y axis --> Z axis
#--------- row 1 ---------------
rotation_matrix_value [ 1 - 1 ] [ 1 - 1 ] \
= 1.0
rotation_matrix_value [ 1 - 1 ] [ 2 - 1 ] \
= 0.0
rotation_matrix_value [ 1 - 1 ] [ 3 - 1 ] \
= 0.0
#---------- row 2 --------------
rotation_matrix_value [ 2 - 1 ] [ 1 - 1 ] \
= 0.0
rotation_matrix_value [ 2 - 1 ] [ 2 - 1 ] \
= math.cos ( arg_rotation_angle_radian )
rotation_matrix_value [ 2 - 1 ] [ 3 - 1 ] \
= 0.0 - math.sin ( arg_rotation_angle_radian )
#---------- row 3 --------------
rotation_matrix_value [ 3 - 1 ] [ 1 - 1 ] \
= 0.0
rotation_matrix_value [ 3 - 1 ] [ 2 - 1 ] \
= math.sin ( arg_rotation_angle_radian )
rotation_matrix_value [ 3 - 1 ] [ 3 - 1 ] \
= math.cos ( arg_rotation_angle_radian )
return_matrixUsingNUMPY \
= NUMPY.array ( rotation_matrix_value )
return return_matrixUsingNUMPY
#-----------------------------------------
return_matrixUsingNUMPY \
= NUMPY.array ( rotation_matrix_value )
return return_matrixUsingNUMPY
----------